parent
d2177912c7
commit
7b05f25b98
6 changed files with 157 additions and 82 deletions
@ -0,0 +1,47 @@ |
||||
/*
|
||||
* Copyright (c) 2009-2021, Google LLC |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are met: |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* * Neither the name of Google LLC nor the |
||||
* names of its contributors may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
#include "upb/alloc.h" |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
// Must be last.
|
||||
#include "upb/port_def.inc" |
||||
|
||||
static void* upb_global_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize, |
||||
size_t size) { |
||||
UPB_UNUSED(alloc); |
||||
UPB_UNUSED(oldsize); |
||||
if (size == 0) { |
||||
free(ptr); |
||||
return NULL; |
||||
} else { |
||||
return realloc(ptr, size); |
||||
} |
||||
} |
||||
|
||||
upb_alloc upb_alloc_global = {&upb_global_allocfunc}; |
@ -0,0 +1,98 @@ |
||||
/*
|
||||
* Copyright (c) 2009-2021, Google LLC |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are met: |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* * Neither the name of Google LLC nor the |
||||
* names of its contributors may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
#ifndef UPB_ALLOC_H_ |
||||
#define UPB_ALLOC_H_ |
||||
|
||||
#include "upb/port_def.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
struct upb_alloc; |
||||
typedef struct upb_alloc upb_alloc; |
||||
|
||||
/* A combined `malloc()`/`free()` function.
|
||||
* If `size` is 0 then the function acts like `free()`, otherwise it acts like |
||||
* `realloc()`. Only `oldsize` bytes from a previous allocation are |
||||
* preserved. */ |
||||
typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize, |
||||
size_t size); |
||||
|
||||
/* A upb_alloc is a possibly-stateful allocator object.
|
||||
* |
||||
* It could either be an arena allocator (which doesn't require individual |
||||
* `free()` calls) or a regular `malloc()` (which does). The client must |
||||
* therefore free memory unless it knows that the allocator is an arena |
||||
* allocator. */ |
||||
struct upb_alloc { |
||||
upb_alloc_func* func; |
||||
}; |
||||
|
||||
UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) { |
||||
UPB_ASSERT(alloc); |
||||
return alloc->func(alloc, NULL, 0, size); |
||||
} |
||||
|
||||
UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize, |
||||
size_t size) { |
||||
UPB_ASSERT(alloc); |
||||
return alloc->func(alloc, ptr, oldsize, size); |
||||
} |
||||
|
||||
UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) { |
||||
UPB_ASSERT(alloc); |
||||
alloc->func(alloc, ptr, 0, 0); |
||||
} |
||||
|
||||
/* The global allocator used by upb. Uses the standard malloc()/free(). */ |
||||
|
||||
extern upb_alloc upb_alloc_global; |
||||
|
||||
/* Functions that hard-code the global malloc.
|
||||
* |
||||
* We still get benefit because we can put custom logic into our global |
||||
* allocator, like injecting out-of-memory faults in debug/testing builds. */ |
||||
|
||||
UPB_INLINE void* upb_gmalloc(size_t size) { |
||||
return upb_malloc(&upb_alloc_global, size); |
||||
} |
||||
|
||||
UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) { |
||||
return upb_realloc(&upb_alloc_global, ptr, oldsize, size); |
||||
} |
||||
|
||||
UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); } |
||||
|
||||
#include "upb/port_undef.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#endif /* UPB_ALLOC_H_ */ |
Loading…
Reference in new issue