forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rudimentary register implementation
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "register.h" | ||
#include "util.h" | ||
|
||
#define REG_SIZE 1024 | ||
|
||
struct Register { | ||
char *data; | ||
size_t len; | ||
size_t size; | ||
}; | ||
|
||
bool register_alloc(Register *reg, size_t size) { | ||
if (size < REG_SIZE) | ||
size = REG_SIZE; | ||
if (reg->size < size) { | ||
reg->data = realloc(reg->data, size); | ||
if (!reg->data) { | ||
reg->size = 0; | ||
reg->len = 0; | ||
return false; | ||
} | ||
reg->size = size; | ||
} | ||
return true; | ||
} | ||
|
||
void register_free(Register *reg) { | ||
if (!reg) | ||
return; | ||
free(reg->data); | ||
reg->data = NULL; | ||
reg->len = 0; | ||
reg->size = 0; | ||
} | ||
|
||
bool register_store(Register *reg, const char *data, size_t len) { | ||
if (!register_alloc(reg, len)) | ||
return false; | ||
memcpy(reg->data, data, len); | ||
reg->len = len; | ||
return true; | ||
} | ||
|
||
bool register_put(Register *reg, Text *txt, Filerange *range) { | ||
size_t len = range->end - range->start; | ||
if (!register_alloc(reg, len)) | ||
return false; | ||
reg->len = text_bytes_get(txt, range->start, len, reg->data); | ||
return true; | ||
} | ||
|
||
bool register_append(Register *reg, Text *txt, Filerange *range) { | ||
size_t rem = reg->size - reg->len; | ||
size_t len = range->end - range->start; | ||
if (len > rem && !register_alloc(reg, reg->size + len - rem)) | ||
return false; | ||
reg->len += text_bytes_get(txt, range->start, len, reg->data + reg->len); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef REGISTER_H | ||
#define REGISTER_H | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include "text.h" | ||
|
||
typedef struct Register Register; | ||
|
||
bool register_alloc(Register *reg, size_t size); | ||
void register_free(Register *reg); | ||
bool register_store(Register *reg, const char *data, size_t len); | ||
bool register_put(Register *reg, Text *txt, Filerange *range); | ||
bool register_append(Register *reg, Text *txt, Filerange *range); | ||
|
||
#endif |