forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add
mem
module to allow tests to override allocators.
This will allow us to do more interesting things with memory allocation within toxcore, and allow fuzzers to explore various allocation failure paths.
- Loading branch information
Showing
7 changed files
with
160 additions
and
44 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,61 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later | ||
* Copyright © 2016-2018 The TokTok team. | ||
* Copyright © 2013 Tox project. | ||
*/ | ||
|
||
#include "mem.h" | ||
|
||
#include <stdlib.h> | ||
|
||
static void *sys_malloc(void *obj, uint32_t size) | ||
{ | ||
return malloc(size); | ||
} | ||
|
||
static void *sys_calloc(void *obj, uint32_t nmemb, uint32_t size) | ||
{ | ||
return calloc(nmemb, size); | ||
} | ||
|
||
static void *sys_realloc(void *obj, void *ptr, uint32_t size) | ||
{ | ||
return realloc(ptr, size); | ||
} | ||
|
||
static void sys_free(void *obj, void *ptr) | ||
{ | ||
free(ptr); | ||
} | ||
|
||
static const Memory_Funcs system_memory_funcs = { | ||
sys_malloc, | ||
sys_calloc, | ||
sys_realloc, | ||
sys_free, | ||
}; | ||
static const Memory system_memory_obj = {&system_memory_funcs}; | ||
|
||
const Memory *system_memory(void) | ||
{ | ||
return &system_memory_obj; | ||
} | ||
|
||
void *mem_malloc(const Memory *mem, uint32_t size) | ||
{ | ||
return mem->funcs->malloc(mem->obj, size); | ||
} | ||
|
||
void *mem_calloc(const Memory *mem, uint32_t nmemb, uint32_t size) | ||
{ | ||
return mem->funcs->calloc(mem->obj, nmemb, size); | ||
} | ||
|
||
void *mem_realloc(const Memory *mem, void *ptr, uint32_t size) | ||
{ | ||
return mem->funcs->realloc(mem->obj, ptr, size); | ||
} | ||
|
||
void mem_free(const Memory *mem, void *ptr) | ||
{ | ||
mem->funcs->free(mem->obj, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later | ||
* Copyright © 2016-2018 The TokTok team. | ||
* Copyright © 2013 Tox project. | ||
*/ | ||
|
||
/** | ||
* Datatypes, functions and includes for the core networking. | ||
*/ | ||
#ifndef C_TOXCORE_TOXCORE_MEM_H | ||
#define C_TOXCORE_TOXCORE_MEM_H | ||
|
||
#include <stdint.h> // uint*_t | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef void *mem_malloc_cb(void *obj, uint32_t size); | ||
typedef void *mem_calloc_cb(void *obj, uint32_t nmemb, uint32_t size); | ||
typedef void *mem_realloc_cb(void *obj, void *ptr, uint32_t size); | ||
typedef void mem_free_cb(void *obj, void *ptr); | ||
|
||
/** @brief Functions wrapping standard C memory allocation functions. */ | ||
typedef struct Memory_Funcs { | ||
mem_malloc_cb *malloc; | ||
mem_calloc_cb *calloc; | ||
mem_realloc_cb *realloc; | ||
mem_free_cb *free; | ||
} Memory_Funcs; | ||
|
||
typedef struct Memory { | ||
const Memory_Funcs *funcs; | ||
void *obj; | ||
} Memory; | ||
|
||
const Memory *system_memory(void); | ||
|
||
void *mem_malloc(const Memory *mem, uint32_t size); | ||
void *mem_calloc(const Memory *mem, uint32_t nmemb, uint32_t size); | ||
void *mem_realloc(const Memory *mem, void *ptr, uint32_t size); | ||
void mem_free(const Memory *mem, void *ptr); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
#endif |
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
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