forked from openziti/tlsuv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes it possible to use custom allocators, for example GC_malloc() when used in a garbage collected project. The code is an adaptation from libuv itself, which supports custom allocators.
- Loading branch information
Showing
13 changed files
with
243 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <uv.h> | ||
#include "common.h" | ||
|
||
typedef struct { | ||
tlsuv_malloc_func malloc; | ||
tlsuv_realloc_func realloc; | ||
tlsuv_calloc_func calloc; | ||
tlsuv_free_func free; | ||
} tlsuv__allocator_t; | ||
|
||
static tlsuv__allocator_t tlsuv__allocator = { | ||
malloc, | ||
realloc, | ||
calloc, | ||
free, | ||
}; | ||
|
||
int tlsuv_replace_allocator(tlsuv_malloc_func malloc_func, | ||
tlsuv_realloc_func realloc_func, | ||
tlsuv_calloc_func calloc_func, | ||
tlsuv_free_func free_func) { | ||
if (malloc_func == NULL || realloc_func == NULL || | ||
calloc_func == NULL || free_func == NULL) { | ||
return UV_EINVAL; | ||
} | ||
|
||
tlsuv__allocator.malloc = malloc_func; | ||
tlsuv__allocator.realloc = realloc_func; | ||
tlsuv__allocator.calloc = calloc_func; | ||
tlsuv__allocator.free = free_func; | ||
|
||
return 0; | ||
} | ||
|
||
void* tlsuv_malloc(size_t size) { | ||
if (size > 0) | ||
return tlsuv__allocator.malloc(size); | ||
return NULL; | ||
} | ||
|
||
void* tlsuv_realloc(void* ptr, size_t size) { | ||
if (size > 0) | ||
return tlsuv__allocator.realloc(ptr, size); | ||
tlsuv_free(ptr); | ||
return NULL; | ||
} | ||
|
||
|
||
void* tlsuv_calloc(size_t count, size_t size) { | ||
return tlsuv__allocator.calloc(count, size); | ||
} | ||
|
||
void tlsuv_free(void* ptr) { | ||
int saved_errno; | ||
|
||
/* The system allocator the assumption that errno is not modified but custom | ||
* allocators may not be so careful. | ||
*/ | ||
saved_errno = errno; | ||
tlsuv__allocator.free(ptr); | ||
errno = saved_errno; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
#include <stdlib.h> | ||
|
||
typedef void* (*tlsuv_malloc_func)(size_t size); | ||
typedef void* (*tlsuv_malloc_func)(size_t size); | ||
typedef void* (*tlsuv_realloc_func)(void* ptr, size_t size); | ||
typedef void* (*tlsuv_calloc_func)(size_t count, size_t size); | ||
typedef void (*tlsuv_free_func)(void* ptr); | ||
|
||
int tlsuv_replace_allocator(tlsuv_malloc_func malloc_func, | ||
tlsuv_realloc_func realloc_func, | ||
tlsuv_calloc_func calloc_func, | ||
tlsuv_free_func free_func); | ||
|
||
void *tlsuv_malloc(size_t size); | ||
void *tlsuv_realloc(void* ptr, size_t size); | ||
void *tlsuv_calloc(size_t count, size_t size); | ||
void tlsuv_free(void* ptr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.