dstrings now have individual memory management functions

This commit is contained in:
Bill Currie 2003-12-22 23:35:22 +00:00
parent e53967b346
commit 5eff81b005
2 changed files with 67 additions and 11 deletions

View file

@ -30,18 +30,31 @@
#define __dstring_h
#include <stdarg.h>
#include <stdlib.h>
typedef struct dstring_mem_s {
void *(*alloc) (void *data, size_t size);
void (*free) (void *data, void *ptr);
void *(*realloc) (void *data, void *ptr, size_t size);
void *data;
} dstring_mem_t;
typedef struct dstring_s {
dstring_mem_t *mem;
unsigned long int size, truesize;
char *str;
} dstring_t;
extern dstring_mem_t dstring_default_mem;
// General buffer functions
//@{
/** Create a new dstring. size and truesize start at 0 and no string buffer
is allocated.
*/
dstring_t *_dstring_new(dstring_mem_t *mem);
dstring_t *dstring_new(void);
//@}
/** Delete a dstring. Both the string buffer and dstring object are freed.
*/
void dstring_delete (dstring_t *dstr);
@ -79,10 +92,13 @@ void dstring_replace (dstring_t *dstr, unsigned int pos, unsigned int rlen,
char *dstring_freeze (dstring_t *dstr);
// String-specific functions
//@{
/** Allocate a new dstring pre-initialized as a null terminated string. size
will be 1 and the first byte 0.
*/
dstring_t *_dstring_newstr (dstring_mem_t *mem);
dstring_t *dstring_newstr (void);
//@}
/** Copy the null terminated string into the dstring. Replaces any existing
data.
The dstring does not have to be null terminated but will become so.

View file

@ -39,24 +39,56 @@ static __attribute__ ((unused)) const char rcsid[] =
#include "compat.h"
dstring_t *
dstring_new (void)
static void *
dstring_alloc (void *data, size_t size)
{
return calloc (1, size);
}
static void
dstring_free (void *data, void *ptr)
{
free (ptr);
}
static void *
dstring_realloc (void *data, void *ptr, size_t size)
{
return realloc (ptr, size);
}
dstring_mem_t dstring_default_mem = {
dstring_alloc,
dstring_free,
dstring_realloc,
0
};
inline dstring_t *
_dstring_new (dstring_mem_t *mem)
{
dstring_t *new;
new = calloc (1, sizeof (dstring_t));
new = mem->alloc (mem->data, sizeof (dstring_t));
if (!new)
Sys_Error ("dstring_new: Failed to allocate memory.");
new->mem = mem;
return new;
}
dstring_t *
dstring_new (void)
{
return _dstring_new (&dstring_default_mem);
}
void
dstring_delete (dstring_t *dstr)
{
if (dstr->str)
free (dstr->str);
free (dstr);
dstr->mem->free (dstr->mem->data, dstr->str);
dstr->mem->free (dstr->mem->data, dstr);
}
inline void
@ -64,7 +96,8 @@ dstring_adjust (dstring_t *dstr)
{
if (dstr->size > dstr->truesize) {
dstr->truesize = (dstr->size + 1023) & ~1023;
dstr->str = realloc (dstr->str, dstr->truesize);
dstr->str = dstr->mem->realloc (dstr->mem->data, dstr->str,
dstr->truesize);
if (!dstr->str)
Sys_Error ("dstring_adjust: Failed to reallocate memory.");
}
@ -149,26 +182,33 @@ dstring_replace (dstring_t *dstr, unsigned int pos, unsigned int rlen,
char *
dstring_freeze (dstring_t *dstr)
{
char *str = realloc (dstr->str, dstr->size);
free (dstr);
char *str = dstr->mem->realloc (dstr->mem->data, dstr->str, dstr->size);
dstr->mem->free (dstr->mem->data, dstr);
return str;
}
dstring_t *
dstring_newstr (void)
inline dstring_t *
_dstring_newstr (dstring_mem_t *mem)
{
dstring_t *new;
new = calloc (1, sizeof (dstring_t));
new = mem->alloc (mem->data, sizeof (dstring_t));
if (!new)
Sys_Error ("dstring_newstr: Failed to allocate memory.");
new->mem = mem;
new->size = 1;
dstring_adjust (new);
new->str[0] = 0;
return new;
}
dstring_t *
dstring_newstr (void)
{
return _dstring_newstr (&dstring_default_mem);
}
void
dstring_copystr (dstring_t *dstr, const char *str)
{