refactor(lsm): allow modules to import other internal header files
This commit is contained in:
parent
c327be80e9
commit
0548efda97
9 changed files with 54 additions and 4 deletions
61
lsm/src/str/lsm_str.c
Normal file
61
lsm/src/str/lsm_str.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lsm.h"
|
||||
#include "lsm/str_internal.h"
|
||||
|
||||
lsm_error lsm_str_init_zero(lsm_str **ptr) {
|
||||
lsm_str *str = calloc(1, sizeof(lsm_str));
|
||||
|
||||
if (str == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
*ptr = str;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_str_init_prealloc(lsm_str *str, char *s) {
|
||||
str->len = strlen(s);
|
||||
|
||||
if (str->len <= 8) {
|
||||
memcpy(str->data.val, s, str->len);
|
||||
free(s);
|
||||
} else {
|
||||
str->data.ptr = s;
|
||||
}
|
||||
}
|
||||
|
||||
lsm_error lsm_str_init(lsm_str **ptr, char *s) {
|
||||
lsm_str *str = calloc(1, sizeof(lsm_str));
|
||||
|
||||
if (str == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
lsm_str_init_prealloc(str, s);
|
||||
|
||||
*ptr = str;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_str_zero(lsm_str *str) {
|
||||
if (str->len > 8) {
|
||||
free(str->data.ptr);
|
||||
}
|
||||
|
||||
str->len = 0;
|
||||
}
|
||||
|
||||
void lsm_str_free(lsm_str *str) {
|
||||
if (str->len > 8) {
|
||||
free(str->data.ptr);
|
||||
}
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
uint64_t lsm_str_len(lsm_str *str) { return str->len; }
|
||||
Loading…
Add table
Add a link
Reference in a new issue