Skip to content

Commit

Permalink
Rename _X to X_ or X as appropriate. (_X is technically reserved by t…
Browse files Browse the repository at this point in the history
…he C standard.)
  • Loading branch information
duckinator committed Nov 9, 2023
1 parent 5761944 commit 4bc23a0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
12 changes: 6 additions & 6 deletions include/dmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ void dmm_init(DMM_PanicFn *panic_fn);

void dmm_add_memory_region(void *start, size_t length);

void *_dmm_malloc(size_t size, const char function[], const char filename[],
void *dmm_malloc_(size_t size, const char function[], const char filename[],
size_t line);
#define dmm_malloc(size) _dmm_malloc(size, __func__, __FILE__, __LINE__)
#define dmm_malloc(size) dmm_malloc_(size, __func__, __FILE__, __LINE__)

void _dmm_free(void *ptr, const char function[], const char filename[],
void dmm_free_(void *ptr, const char function[], const char filename[],
size_t line);
#define dmm_free(ptr) _dmm_free(ptr, __func__, __FILE__, __LINE__)
#define dmm_free(ptr) dmm_free_(ptr, __func__, __FILE__, __LINE__)

void *_dmm_realloc(void *ptr, size_t size, const char function[],
void *dmm_realloc_(void *ptr, size_t size, const char function[],
const char filename[], size_t line);
#define dmm_realloc(ptr, size) _dmm_realloc(ptr, size, __func__, __FILE__, __LINE__)
#define dmm_realloc(ptr, size) dmm_realloc_(ptr, size, __func__, __FILE__, __LINE__)

// Tests
void add_dmm_tests(void);
Expand Down
12 changes: 6 additions & 6 deletions include/dmm_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

void *dmm_instance_add_memory_region(void *instance, void *start, size_t length);

void *_dmm_instance_malloc(void *instance, size_t size, const char function[],
void *dmm_instance_malloc_(void *instance, size_t size, const char function[],
const char filename[], size_t line);
#define dmm_instance_malloc(instance, size) _dmm_instance_malloc(instance, size, __func__, __FILE__, __LINE__)
#define dmm_instance_malloc(instance, size) dmm_instance_malloc_(instance, size, __func__, __FILE__, __LINE__)

void _dmm_instance_free(void *instance, void *ptr, const char function[],
void dmm_instance_free_(void *instance, void *ptr, const char function[],
const char filename[], size_t line);
#define dmm_instance_free(instance, ptr) _dmm_instance_free(instance, ptr, __func__, __FILE__, __LINE__)
#define dmm_instance_free(instance, ptr) dmm_instance_free_(instance, ptr, __func__, __FILE__, __LINE__)

void *_dmm_instance_realloc(void *instance, void *ptr, size_t size,
void *dmm_instance_realloc_(void *instance, void *ptr, size_t size,
const char function[], const char filename[], size_t line);
#define dmm_instance_realloc(instance, ptr, size) _dmm_instance_realloc(instance, ptr, size, __func__, __FILE__, __LINE__)
#define dmm_instance_realloc(instance, ptr, size) dmm_instance_realloc_(instance, ptr, size, __func__, __FILE__, __LINE__)


#endif
10 changes: 5 additions & 5 deletions src/dmm_string.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "dmm_string.h"
#include <stddef.h>

void *_dmm_memcpy(void *dest, const void *src, size_t n)
void *dmm_memcpy(void *dest, const void *src, size_t n)
{
char *dest_ = (char*)dest;
char *src_ = (char*)src;
Expand All @@ -13,7 +13,7 @@ void *_dmm_memcpy(void *dest, const void *src, size_t n)
return dest;
}

void *_dmm_memset(void *s, int c, size_t n)
void *dmm_memset(void *s, int c, size_t n)
{
char *s_ = (char*)s;
for (size_t i = 0; i < n; i++) {
Expand All @@ -24,7 +24,7 @@ void *_dmm_memset(void *s, int c, size_t n)
}

// Similar to strncpy(), except it sets the remaining bytes to null.
void _dmm_strncpy_null(char *dest, const char *src, size_t destsize)
void dmm_strncpy_null(char *dest, const char *src, size_t destsize)
{
size_t srcsize = 0;
size_t min;
Expand All @@ -36,9 +36,9 @@ void _dmm_strncpy_null(char *dest, const char *src, size_t destsize)
min = (srcsize < destsize) ? srcsize : destsize;

// Initialize destination buffer to null bytes.
_dmm_memset(dest, 0, destsize);
dmm_memset(dest, 0, destsize);
// Copy src buffer to destination buffer.
_dmm_memcpy(dest, src, min);
dmm_memcpy(dest, src, min);
// Terminate with a null byte.
dest[destsize - 1] = 0;
}
6 changes: 3 additions & 3 deletions src/dmm_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stddef.h>

void *_dmm_memcpy(void *dest, const void *src, size_t n);
void *_dmm_memset(void *s, int c, size_t n);
void _dmm_strncpy_null(char *dest, const char *src, size_t destsize);
void *dmm_memcpy(void *dest, const void *src, size_t n);
void *dmm_memset(void *s, int c, size_t n);
void dmm_strncpy_null(char *dest, const char *src, size_t destsize);

#endif
26 changes: 13 additions & 13 deletions src/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
void dmm_call_location(DMM_CallLocation *call_location,
const char function[], const char filename[], size_t line)
{
_dmm_strncpy_null(call_location->function, function,
dmm_strncpy_null(call_location->function, function,
sizeof(call_location->function));
_dmm_strncpy_null(call_location->filename, filename,
dmm_strncpy_null(call_location->filename, filename,
sizeof(call_location->filename));

call_location->line = line;
Expand Down Expand Up @@ -116,7 +116,7 @@ DMM_MallocHeader *dmm_instance_get_first_free_chunk(void *instance, size_t size)
return NULL;
}

void *_dmm_instance_malloc(void *instance, size_t size, const char function[],
void *dmm_instance_malloc_(void *instance, size_t size, const char function[],
const char filename[], size_t line)
{
DMM_MallocHeader *result = dmm_instance_get_first_free_chunk(instance, size);
Expand All @@ -139,14 +139,14 @@ void *_dmm_instance_malloc(void *instance, size_t size, const char function[],

// NOTE: dmm_malloc() zeroes memory, which isn't what most allocators do.
// Not sure if that violates the C standard or not, but *shrug*.
_dmm_memset(result->data, 0, size);
dmm_memset(result->data, 0, size);

// Create a new header after the data, if the remaining data size is
// large enough to fit the header.
if (next_size > sizeof(DMM_MallocHeader)) {
void *next = (void *)((size_t)(result + 1) + size);
DMM_MallocHeader *next_header = (DMM_MallocHeader*)next;
_dmm_memset(next, 0, sizeof(DMM_MallocHeader));
dmm_memset(next, 0, sizeof(DMM_MallocHeader));

next_header->magic = DMM_HEADER_MAGIC;
next_header->size = next_size;
Expand All @@ -162,7 +162,7 @@ void *_dmm_instance_malloc(void *instance, size_t size, const char function[],
return result->data;
}

void _dmm_instance_free(void *instance, void *ptr, const char function[],
void dmm_instance_free_(void *instance, void *ptr, const char function[],
const char filename[], size_t line)
{
DMM_MallocHeader *header = (DMM_MallocHeader*)(ptr) - 1;
Expand All @@ -184,16 +184,16 @@ void _dmm_instance_free(void *instance, void *ptr, const char function[],
dmm_consolidate(instance);
}

// TODO: Have _dmm_instance_realloc() resize a chunk if possible.
void *_dmm_instance_realloc(void *instance, void *ptr, size_t size, const char *function,
// TODO: Have dmm_instance_realloc_() resize a chunk if possible.
void *dmm_instance_realloc_(void *instance, void *ptr, size_t size, const char *function,
const char *filename, size_t line)
{
DMM_MallocHeader *header;
void *new_ptr;
size_t min_size;

if (ptr == NULL) {
return _dmm_instance_malloc(instance, size, function, filename, line);
return dmm_instance_malloc_(instance, size, function, filename, line);
}

// Check for memory clobbering.
Expand All @@ -203,20 +203,20 @@ void *_dmm_instance_realloc(void *instance, void *ptr, size_t size, const char *
}

// Allocate new memory chunk.
new_ptr = _dmm_instance_malloc(instance, size, function, filename, line);
new_ptr = dmm_instance_malloc_(instance, size, function, filename, line);
if (new_ptr == NULL) {
return NULL;
}

// Zero new memory chunk, to prevent data leakage.
_dmm_memset(new_ptr, 0, size);
dmm_memset(new_ptr, 0, size);

// Copy data to new memory address.
min_size = (size < header->size) ? size : header->size;
_dmm_memcpy(new_ptr, ptr, min_size);
dmm_memcpy(new_ptr, ptr, min_size);

// Release the original allocation.
_dmm_instance_free(instance, ptr, function, filename, line);
dmm_instance_free_(instance, ptr, function, filename, line);

return new_ptr;
}
12 changes: 6 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ DMM_MallocHeader *dmm_get_first_free_chunk(size_t size)
return dmm_instance_get_first_free_chunk(dmm_global_instance, size);
}

void *_dmm_malloc(size_t size, const char function[], const char filename[],
void *dmm_malloc_(size_t size, const char function[], const char filename[],
size_t line)
{
return _dmm_instance_malloc(dmm_global_instance, size, function,
return dmm_instance_malloc_(dmm_global_instance, size, function,
filename, line);
}

void _dmm_free(void *ptr, const char function[], const char filename[],
void dmm_free_(void *ptr, const char function[], const char filename[],
size_t line)
{
_dmm_instance_free(dmm_global_instance, ptr, function, filename, line);
dmm_instance_free_(dmm_global_instance, ptr, function, filename, line);
}

void *_dmm_realloc(void *ptr, size_t size, const char function[],
void *dmm_realloc_(void *ptr, size_t size, const char function[],
const char filename[], size_t line)
{
return _dmm_instance_realloc(dmm_global_instance, ptr, size, function,
return dmm_instance_realloc_(dmm_global_instance, ptr, size, function,
filename, line);
}
4 changes: 2 additions & 2 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void test_dmm_instance_add_region(void);
void test_dmm_instance_malloc(void);
void test_dmm_instance_free_sets_header(void);

noreturn void _panic(const char *message, const char *function,
noreturn void panic(const char *message, const char *function,
const char *filename, size_t line)
{
// loosely copy Rust's panic message format.
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(void)
tinker_add_test(dmm_instance_malloc);
tinker_add_test(dmm_instance_free_sets_header);

dmm_init(&_panic);
dmm_init(&panic);

init_mmap();
if (!tinker_run_tests(&putchar)) {
Expand Down

0 comments on commit 4bc23a0

Please sign in to comment.