Switch to using Allocator in dstr
This commit is contained in:
parent
39c88505bd
commit
2e93bd794a
@ -2,6 +2,7 @@
|
|||||||
#define DSTR_H
|
#define DSTR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -9,8 +10,8 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct dstr String;
|
typedef struct dstr String;
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity);
|
String *wapp_dstr_with_capacity(u64 capacity, Allocator *allocator);
|
||||||
String *wapp_dstr_from_string(const char *str);
|
String *wapp_dstr_from_string(const char *str, Allocator *allocator);
|
||||||
void wapp_dstr_update(String **dst, const char *src);
|
void wapp_dstr_update(String **dst, const char *src);
|
||||||
void wapp_dstr_free(String **str);
|
void wapp_dstr_free(String **str);
|
||||||
void wapp_dstr_concat(String **dst, const char *src);
|
void wapp_dstr_concat(String **dst, const char *src);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "dstr.h"
|
#include "dstr.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
|
#include "mem_libc.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -9,24 +11,34 @@
|
|||||||
#define CAPACITY_SCALAR 8
|
#define CAPACITY_SCALAR 8
|
||||||
|
|
||||||
struct dstr {
|
struct dstr {
|
||||||
|
Allocator allocator;
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
u64 size;
|
u64 size;
|
||||||
char buf[];
|
char buf[];
|
||||||
};
|
};
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity) {
|
String *wapp_dstr_with_capacity(u64 capacity, Allocator *allocator) {
|
||||||
String *out = (String *)calloc(1, sizeof(String) + capacity + 1);
|
Allocator alloc;
|
||||||
|
if (allocator) {
|
||||||
|
alloc = *allocator;
|
||||||
|
} else {
|
||||||
|
alloc = wapp_mem_libc_allocator();
|
||||||
|
}
|
||||||
|
|
||||||
|
String *out =
|
||||||
|
(String *)wapp_mem_allocator_alloc(&alloc, sizeof(String) + capacity + 1);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out->allocator = alloc;
|
||||||
out->capacity = capacity;
|
out->capacity = capacity;
|
||||||
out->size = 0;
|
out->size = 0;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
String *wapp_dstr_from_string(const char *str) {
|
String *wapp_dstr_from_string(const char *str, Allocator *allocator) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -35,14 +47,13 @@ String *wapp_dstr_from_string(const char *str) {
|
|||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
String *out = wapp_dstr_with_capacity(capacity);
|
String *out = wapp_dstr_with_capacity(capacity, allocator);
|
||||||
|
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->size = length;
|
out->size = length;
|
||||||
strncpy(out->buf, str, length);
|
strncpy(out->buf, str, length + 1);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@ -56,24 +67,24 @@ void wapp_dstr_update(String **dst, const char *src) {
|
|||||||
|
|
||||||
String *str = *dst;
|
String *str = *dst;
|
||||||
|
|
||||||
if (length <= str->capacity) {
|
if (length < str->capacity) {
|
||||||
memset(str->buf, 0, str->capacity);
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
str->size = length;
|
str->size = length;
|
||||||
|
|
||||||
strncpy(str->buf, src, length);
|
strncpy(str->buf, src, length + 1);
|
||||||
} else {
|
} else {
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
String *tmp = (String *)realloc(*dst, sizeof(String) + capacity + 1);
|
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||||
|
&(str->allocator), *dst, sizeof(String) + capacity + 1);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
tmp->capacity = capacity;
|
||||||
tmp->size = length;
|
tmp->size = length;
|
||||||
strncpy(tmp->buf, src, length);
|
strncpy(tmp->buf, src, length + 1);
|
||||||
|
|
||||||
*dst = tmp;
|
*dst = tmp;
|
||||||
}
|
}
|
||||||
@ -84,8 +95,8 @@ void wapp_dstr_free(String **str) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(*str);
|
String *str_ptr = *str;
|
||||||
*str = NULL;
|
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_dstr_concat(String **dst, const char *src) {
|
void wapp_dstr_concat(String **dst, const char *src) {
|
||||||
@ -131,10 +142,11 @@ void wapp_dstr_resize(String **str) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String *str_ptr = *str;
|
||||||
u64 capacity = (*str)->size;
|
u64 capacity = (*str)->size;
|
||||||
|
|
||||||
String *tmp = (String *)realloc(*str, sizeof(String) + capacity + 1);
|
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||||
|
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user