diff options
author | Freya Murphy <freya@freyacat.org> | 2024-09-09 20:48:08 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-09-09 20:48:08 -0400 |
commit | 0ff948af3d65150f44c9fe801ada806ce0637fe1 (patch) | |
tree | ba766d33d12bf1063660c2144af2d5f5c36b4c87 /masm/strtbl.c | |
parent | i forgor syscall (diff) | |
download | mips-0ff948af3d65150f44c9fe801ada806ce0637fe1.tar.gz mips-0ff948af3d65150f44c9fe801ada806ce0637fe1.tar.bz2 mips-0ff948af3d65150f44c9fe801ada806ce0637fe1.zip |
relocation table hell
Diffstat (limited to '')
-rw-r--r-- | masm/strtbl.c | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/masm/strtbl.c b/masm/strtbl.c index b01bb92..7bdbbea 100644 --- a/masm/strtbl.c +++ b/masm/strtbl.c @@ -6,44 +6,49 @@ int strtbl_get_str(struct str_table *str_tbl, const char *str, size_t *res) { - for (size_t i = 0; i < str_tbl->size; i ++) { - if (strcmp(str_tbl->ptr + i, str) == 0) { - if (res != NULL) - *res = i; - return M_SUCCESS; - } - } + for (size_t i = 0; i < str_tbl->size; i ++) { + if (strcmp(str_tbl->ptr + i, str) == 0) { + if (res != NULL) + *res = i; + return M_SUCCESS; + } + } - return M_ERROR; + return M_ERROR; } int strtbl_write_str(struct str_table *str_tbl, const char *str, size_t *res) { - if (strtbl_get_str(str_tbl, str, res) == M_SUCCESS) - return M_SUCCESS; + if (strtbl_get_str(str_tbl, str, res) == M_SUCCESS) + return M_SUCCESS; - size_t len = strlen(str); - char *new = realloc(str_tbl->ptr, str_tbl->size + len + 1); - if (new == NULL) - return M_ERROR; - str_tbl->ptr = new; - memcpy(str_tbl->ptr + str_tbl->size, str, len + 1); + size_t len = strlen(str); + char *new = realloc(str_tbl->ptr, str_tbl->size + len + 1); + if (new == NULL) + return M_ERROR; + str_tbl->ptr = new; + memcpy(str_tbl->ptr + str_tbl->size, str, len + 1); - if (res != NULL) - *res = str_tbl->size; + if (res != NULL) + *res = str_tbl->size; - str_tbl->size += len + 1; - return M_SUCCESS; + str_tbl->size += len + 1; + return M_SUCCESS; } -void strtbl_init(struct str_table *str_tbl) +int strtbl_init(struct str_table *str_tbl) { - str_tbl->size = 1; - str_tbl->ptr = malloc(1); - *str_tbl->ptr = '\0'; + str_tbl->size = 1; + str_tbl->ptr = malloc(1); + if (str_tbl->ptr == NULL) { + ERROR("cannot alloc"); + return M_ERROR; + } + *str_tbl->ptr = '\0'; + return M_SUCCESS; } void strtbl_free(struct str_table *str_tbl) { - free(str_tbl->ptr); + free(str_tbl->ptr); } |