Skip to content

Commit

Permalink
Allow custom memory management
Browse files Browse the repository at this point in the history
Provides functions to replace standard malloc/calloc/realloc/free

Signed-off-by: Francis Bouvier <[email protected]>
  • Loading branch information
francisbouvier committed Mar 19, 2024
1 parent 6266091 commit 2507f64
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
22 changes: 22 additions & 0 deletions bindings/hubbub/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright 2007 John-Mark Bell <[email protected]>
*/

#include <stdlib.h>

#ifndef dom_hubbub_utils_h_
#define dom_hubbub_utils_h_

Expand All @@ -25,4 +27,24 @@
#define UNUSED(x) ((void)(x))
#endif

#ifndef malloc_custom
#define malloc_custom

typedef void *(*custom_malloc)(size_t);
typedef void *(*custom_realloc)(void*, size_t);
typedef void *(*custom_calloc)(size_t, size_t);
typedef void (*custom_free)(void*);

void *m_alloc(size_t size);
void *re_alloc(void *ptr, size_t size);
void *c_alloc(size_t elementCount, size_t size);
void f_ree(void *ptr);

#define malloc(size) m_alloc(size)
#define realloc(ptr, size) re_alloc(ptr, size)
#define calloc(elementCount, size) c_alloc(elementCount, size)
#define free(ptr) f_ree(ptr)

#endif

#endif
21 changes: 21 additions & 0 deletions bindings/xml/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright 2007 John-Mark Bell <[email protected]>
*/

#include <stdlib.h>

#ifndef xml_utils_h_
#define xml_utils_h_

Expand All @@ -25,4 +27,23 @@
#define UNUSED(x) ((void)(x))
#endif

#ifndef malloc_custom
#define malloc_custom
typedef void *(*custom_malloc)(size_t);
typedef void *(*custom_realloc)(void*, size_t);
typedef void *(*custom_calloc)(size_t, size_t);
typedef void (*custom_free)(void*);

void *m_alloc(size_t size);
void *re_alloc(void *ptr, size_t size);
void *c_alloc(size_t elementCount, size_t size);
void f_ree(void *ptr);

#define malloc(size) m_alloc(size)
#define realloc(ptr, size) re_alloc(ptr, size)
#define calloc(elementCount, size) c_alloc(elementCount, size)
#define free(ptr) f_ree(ptr)

#endif

#endif
1 change: 1 addition & 0 deletions src/events/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <dom/events/event.h>

#include "utils/list.h"
#include "utils/utils.h"

/* The private virtual table */
struct dom_event_private_vtable {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sources
DIR_SOURCES := namespace.c hashtable.c character_valid.c validate.c walk.c
DIR_SOURCES := namespace.c hashtable.c character_valid.c validate.c walk.c utils.c

include $(NSBUILD)/Makefile.subdir
1 change: 1 addition & 0 deletions src/utils/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#endif
#include "utils/hashtable.h"
#include "utils/utils.h"

#include <libwapcaplet/libwapcaplet.h>

Expand Down
23 changes: 23 additions & 0 deletions src/utils/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdlib.h>
#include "utils.h"

custom_malloc my_malloc = malloc;
custom_realloc my_realloc = realloc;
custom_calloc my_calloc = calloc;
custom_free my_free = free;

void *m_alloc(size_t size)
{
return my_malloc(size);
}
void *re_alloc(void *ptr, size_t size) {
return my_realloc(ptr, size);
}
void *c_alloc(size_t elementCount, size_t size)
{
return my_calloc(elementCount, size);
}
void f_ree(void *ptr)
{
my_free(ptr);
}
22 changes: 22 additions & 0 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright 2007 John-Mark Bell <[email protected]>
*/

#include <stdlib.h>

#ifndef dom_utils_utils_h_
#define dom_utils_utils_h_

Expand All @@ -25,4 +27,24 @@
#define UNUSED(x) ((void)(x))
#endif

#ifndef malloc_custom
#define malloc_custom

typedef void *(*custom_malloc)(size_t);
typedef void *(*custom_realloc)(void*, size_t);
typedef void *(*custom_calloc)(size_t, size_t);
typedef void (*custom_free)(void*);

void *m_alloc(size_t size);
void *re_alloc(void *ptr, size_t size);
void *c_alloc(size_t elementCount, size_t size);
void f_ree(void *ptr);

#define malloc(size) m_alloc(size)
#define realloc(ptr, size) re_alloc(ptr, size)
#define calloc(elementCount, size) c_alloc(elementCount, size)
#define free(ptr) f_ree(ptr)

#endif

#endif

0 comments on commit 2507f64

Please sign in to comment.