src: add help param in supported_params #1

- Add allocation wrapper
- Use wrapper in all sources
This commit is contained in:
tylen
2024-09-24 20:09:24 +00:00
committed by Vasily Davydov
parent 15d29d0a9e
commit dd15119a08
5 changed files with 91 additions and 23 deletions

View File

@@ -0,0 +1,53 @@
#ifndef __ALLOC_WARPPERS_H__
#define __ALLOC_WARPPERS_H__
#include "log.h"
#include <stdlib.h>
#define FAILED_ALLOC_MESSAGE \
LOG_ERROR("Allocation failed. Returned pointer is NULL.");
#define cdo_malloc(size) ((void*)({ \
void *__ptr__ = malloc(size); \
if (!__ptr__) { \
FAILED_ALLOC_MESSAGE; \
} \
__ptr__; \
}))
#define cdo_free(ptr) do { \
if (!ptr) { \
LOG_WARNING("Freeing NULL pointer."); \
} \
free(ptr); \
} while (0)
#define cdo_calloc(nmemb, size) ((void*)({ \
void *__ptr__ = calloc(nmemb, size); \
if (!__ptr__) { \
FAILED_ALLOC_MESSAGE; \
} \
__ptr__; \
}))
#define cdo_realloc(ptr, size) ((void*)({ \
void *__ptr__ = realloc(ptr, size); \
if (!__ptr__) { \
FAILED_ALLOC_MESSAGE; \
} \
__ptr__; \
}))
#define cdo_reallocarray(ptr, nmemb, size) ((void*)({ \
void *__ptr__ = reallocarray(ptr, nmemb, size); \
if (!__ptr__) { \
FAILED_ALLOC_MESSAGE; \
} \
__ptr__; \
}))
#endif /*__ALLOC_WARPPERS_H__*/

View File

@@ -1,12 +1,13 @@
#include "buffer.h"
#include "alloc_wrappers.h"
#include "log.h"
#include <string.h>
#include <stdio.h>
Buffer* buffer_create(size_t capacity) {
Buffer* buffer = (Buffer*)malloc(sizeof(Buffer));
buffer->data = (char*)malloc(capacity);
Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer));
buffer->data = (char*)cdo_malloc(capacity);
buffer->size = 0;
buffer->capacity = capacity;
return buffer;
@@ -16,7 +17,7 @@ void buffer_append(Buffer* buffer, const char* str) {
size_t len = strlen(str);
if (buffer->size + len >= buffer->capacity) {
buffer->capacity *= 2;
buffer->data = (char*)realloc(buffer->data, buffer->capacity);
buffer->data = (char*)cdo_realloc(buffer->data, buffer->capacity);
}
strcpy(buffer->data + buffer->size, str);
buffer->size += len;
@@ -31,9 +32,9 @@ void buffer_free(Buffer* buffer) {
if (!buffer->data || !buffer) {
LOG_WARNING("Trying to free NULL pointer");
}
free(buffer->data);
cdo_free(buffer->data);
buffer->data = NULL;
free(buffer);
cdo_free(buffer);
buffer = NULL;
}