memory allocation config via "sc_config.h"

This commit is contained in:
tezc 2021-01-23 20:57:08 +03:00
parent f077d9870e
commit 20803c9a3b
15 changed files with 79 additions and 63 deletions

View File

@ -32,6 +32,13 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_array_realloc realloc
#define sc_array_free free
#endif
/**
* Internals, do not use
*/
@ -53,15 +60,6 @@ bool sc_array_expand(void **arr, size_t elem_size);
* Internal End.
*/
/**
* Configure memory allocators here. You can plug your allocator if you want,
* replace 'realloc' and 'free' with your allocator, make sure you include
* new allocator header.
*/
#define sc_array_realloc realloc
#define sc_array_free free
/**
* @param arr Array pointer
* @param cap Initial capacity, '0' is accepted, then no memory allocation

View File

@ -31,6 +31,15 @@
#include <stdlib.h>
#include <string.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_buf_malloc malloc
#define sc_buf_realloc realloc
#define sc_buf_free free
#endif
#define SC_BUF_CORRUPT 1u
#define SC_BUF_OOM 3u
@ -50,10 +59,6 @@ struct sc_buf
bool ref;
};
#define sc_buf_malloc malloc
#define sc_buf_realloc realloc
#define sc_buf_free free
/**
* Create buffer
*

View File

@ -30,6 +30,14 @@
#include <stddef.h>
#include <stdint.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_heap_malloc malloc
#define sc_heap_realloc realloc
#define sc_heap_free free
#endif
struct sc_heap_data
{
@ -44,13 +52,6 @@ struct sc_heap
struct sc_heap_data *elems;
};
/**
* Plug your memory allocator.
*/
#define sc_heap_malloc malloc
#define sc_heap_realloc realloc
#define sc_heap_free free
/**
* @param heap Heap
* @param cap Initial capacity, pass '0' for no initial memory allocation

View File

@ -31,6 +31,13 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_map_calloc calloc
#define sc_map_free free
#endif
#define sc_map_of_strkey(name, K, V) \
struct sc_map_item_##name \
{ \
@ -85,9 +92,6 @@
for ((V) = (map)->mem[__i].value, __b = 1; \
__b && (map)->mem[__i].key != 0; __b = 0)
#define sc_map_calloc calloc
#define sc_map_free free
// clang-format off
// name key type value type

View File

@ -31,13 +31,13 @@
#include <string.h>
#if (SIZE_MAX == 0xFFFF)
#define SIZE_T_BITS 16
#define SIZE_T_BITS 16
#elif (SIZE_MAX == 0xFFFFFFFF)
#define SIZE_T_BITS 32
#define SIZE_T_BITS 32
#elif (SIZE_MAX == 0xFFFFFFFFFFFFFFFF)
#define SIZE_T_BITS 64
#define SIZE_T_BITS 64
#else
#error unknown size_t bits
#error unknown size_t bits
#endif
bool sc_math_is_pow2(size_t num)

View File

@ -32,6 +32,13 @@
#include <stdlib.h>
#include <string.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_queue_realloc realloc
#define sc_queue_free free
#endif
/**
* Internals, do not use
*/
@ -84,12 +91,6 @@ bool sc_queue_init(void **q, size_t elem_size, size_t cap);
void sc_queue_term(void **q);
bool sc_queue_expand(void **q, size_t elem_size);
/**
* Plug your allocator if you want.
*/
#define sc_queue_realloc realloc
#define sc_queue_free free
/**
* @param q Queue pointer
* @param count Initial capacity, '0' is a valid value if you don't want to

View File

@ -1018,6 +1018,14 @@ int sc_sock_poll_del(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
struct epoll_event ep_ev = {.data.ptr = data,
.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP};
if (fdt->op == SC_SOCK_NONE) {
return 0;
}
if ((fdt->op & events) == 0) {
}
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
return 0;
}

View File

@ -41,7 +41,7 @@ typedef SOCKET sc_sock_int;
typedef int sc_sock_int;
#endif
#define SC_SOCK_BUF_SIZE 8192
#define SC_SOCK_BUF_SIZE 32768
enum sc_sock_rc

View File

@ -51,14 +51,16 @@ struct sc_str
#define sc_str_bytes(n) ((n) + sizeof(struct sc_str) + 1)
#ifndef SC_SIZE_MAX
#define SC_SIZE_MAX (UINT32_MAX - sizeof(struct sc_str) - 1)
#define SC_SIZE_MAX (UINT32_MAX - sizeof(struct sc_str) - 1)
#endif
char *sc_str_create(const char *str)
{
size_t size;
assert(str != NULL);
size_t size = strlen(str);
size = strlen(str);
if (size > SC_SIZE_MAX) {
return NULL;
}
@ -68,10 +70,10 @@ char *sc_str_create(const char *str)
char *sc_str_create_len(const char *str, uint32_t len)
{
assert(str != NULL);
struct sc_str *copy;
assert(str != NULL);
copy = sc_str_malloc(sc_str_bytes(len));
if (copy == NULL) {
return NULL;

View File

@ -30,6 +30,14 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_str_malloc malloc
#define sc_str_realloc realloc
#define sc_str_free free
#endif
/**
* Length prefixed C strings, length is at the start of the allocated memory
* e.g :
@ -42,12 +50,6 @@
* additional functionality when it's used with these functions here.
*/
/**
* Plug your allocators.
*/
#define sc_str_malloc malloc
#define sc_str_realloc realloc
#define sc_str_free free
/**
* @param str '\0' terminated C string, must not be NULL.

View File

@ -144,10 +144,6 @@ int sc_time_sleep(uint64_t milliseconds)
rc = nanosleep(&t, &rem);
} while (rc != 0 && errno != EINTR);
if (rc != 0) {
sc_time_on_error("nanosleep() : %s ", strerror(errno));
}
return rc;
#endif
}

View File

@ -26,14 +26,6 @@
#include <stdint.h>
/**
* If you want to log or abort on errors, put your error function here.
* It will be called with printf like error msg.
*
* my_on_error(const char* fmt, ...);
*/
#define sc_time_on_error(...)
/**
* This is not a monotonic timer.
* @return Current timestamp in milliseconds.

View File

@ -212,4 +212,3 @@ uint64_t sc_timer_timeout(struct sc_timer *timer, uint64_t timestamp, void *arg,
return min(TICK - time, TICK);
}

View File

@ -30,6 +30,13 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_timer_malloc malloc
#define sc_timer_free free
#endif
#define SC_TIMER_INVALID UINT64_MAX
struct sc_timer_data
@ -48,9 +55,6 @@ struct sc_timer
struct sc_timer_data *list;
};
#define sc_timer_malloc malloc
#define sc_timer_free free
/**
* @param timer Timer
* @param timestamp Current timestamp. Use monotonic timer source.

View File

@ -24,6 +24,13 @@
#ifndef SC_URL_H
#define SC_URL_H
#ifdef SC_HAVE_CONFIG_H
#include "sc_config.h"
#else
#define sc_url_malloc malloc
#define sc_url_free free
#endif
#include <stdbool.h>
#include <stdlib.h>
@ -60,9 +67,6 @@ struct sc_url
char buf[];
};
#define sc_url_malloc malloc
#define sc_url_free free
struct sc_url *sc_url_create(const char *str);
void sc_url_destroy(struct sc_url *url);