my old ralloc...
From Newsgroup: comp.lang.c
Managed to find my old ralloc code.
My code:
_______________________
#define RALLOC_NDEBUG // No debug output
/* Chris M. Thomasson's Region Allocator __________________________________________*/
#if ! defined (RALLOC_H)
# define RALLOC_H
# if defined (__cplusplus)
extern "C" {
# endif
/**************************************************************/
#include <stddef.h>
#include <assert.h>
#if defined (_MSC_VER)
/* warning C4116: unnamed type definition in parentheses */
# pragma warning (disable : 4116)
#endif
#if ! defined (RALLOC_NDEBUG)
# include <stdio.h>
# define RALLOC_DBG_PRINTF(mp_exp) printf mp_exp
#else
# define RALLOC_DBG_PRINTF(mp_exp) ((void)0)
#endif
#if ! defined (RALLOC_UINTPTR_TYPE)
# define RALLOC_UINTPTR_TYPE size_t
#endif
typedef RALLOC_UINTPTR_TYPE ralloc_uintptr_type;
typedef char ralloc_static_assert[
sizeof(ralloc_uintptr_type) == sizeof(void*) ? 1 : -1
];
enum ralloc_align_enum {
ALIGN_ENUM
};
struct ralloc_align_struct {
char pad;
double type;
};
union ralloc_align_max {
char char_;
short int short_;
int int_;
long int long_;
float float_;
double double_;
long double long_double_;
void* ptr_;
void* (*fptr_) (void*);
enum ralloc_align_enum enum_;
struct ralloc_align_struct struct_;
size_t size_t_;
ptrdiff_t ptrdiff_t;
};
#define RALLOC_ALIGN_OF(mp_type) \
offsetof( \
struct { \
char pad_RALLOC_ALIGN_OF; \
mp_type type_RALLOC_ALIGN_OF; \
}, \
type_RALLOC_ALIGN_OF \
)
#define RALLOC_ALIGN_MAX RALLOC_ALIGN_OF(union ralloc_align_max)
#define RALLOC_ALIGN_UP(mp_ptr, mp_align) \
((void*)( \
(((ralloc_uintptr_type)(mp_ptr)) + ((mp_align) - 1)) \
& ~(((mp_align) - 1)) \
))
#define RALLOC_ALIGN_ASSERT(mp_ptr, mp_align) \
(((void*)(mp_ptr)) == RALLOC_ALIGN_UP(mp_ptr, mp_align))
struct region {
unsigned char* buffer;
size_t size;
size_t offset;
};
static void
rinit(
struct region* const self,
void* buffer,
size_t size
) {
self->buffer = buffer;
self->size = size;
self->offset = 0;
RALLOC_DBG_PRINTF((
"rinit(%p) {\n"
" buffer = %p\n"
" size = %lu\n"
"}\n\n\n",
(void*)self,
buffer,
(unsigned long int)size
));
}
static void*
rallocex(
struct region* const self,
size_t size,
size_t align
) {
unsigned char* align_buffer;
size_t offset = self->offset;
unsigned char* raw_buffer = self->buffer + offset;
if (!size) {
size = 1;
}
if (!align) {
align = RALLOC_ALIGN_MAX;
}
assert(align == 1 || RALLOC_ALIGN_ASSERT(align, 2));
align_buffer = RALLOC_ALIGN_UP(raw_buffer, align);
assert(RALLOC_ALIGN_ASSERT(align_buffer, align));
size += align_buffer - raw_buffer;
if (offset + size > self->size) {
return NULL;
}
self->offset = offset + size;
RALLOC_DBG_PRINTF((
"rallocex(%p) {\n"
" size = %lu\n"
" alignment = %lu\n"
" origin offset = %lu\n"
" final offset = %lu\n"
" raw_buffer = %p\n"
" align_buffer = %p\n"
" size adjustment = %lu\n"
" final size = %lu\n"
"}\n\n\n",
(void*)self,
(unsigned long int)size - (align_buffer - raw_buffer),
(unsigned long int)align,
(unsigned long int)offset,
(unsigned long int)self->offset,
(void*)raw_buffer,
(void*)align_buffer,
(unsigned long int)(align_buffer - raw_buffer),
(unsigned long int)size
));
return align_buffer;
}
#define ralloc(mp_self, mp_size) \
rallocex((mp_self), (mp_size), RALLOC_ALIGN_MAX)
#define ralloct(mp_self, mp_count, mp_type) \
rallocex( \
(mp_self), \
sizeof(mp_type) * (mp_count),\
RALLOC_ALIGN_OF(mp_type) \
)
static void
rflush(
struct region* const self
) {
self->offset = 0;
RALLOC_DBG_PRINTF((
"rflush(%p) {}\n\n\n",
(void*)self
));
}
#undef RALLOC_DBG_PRINTF
#undef RALLOC_UINTPTR_TYPE
/**************************************************************/
# if defined (__cplusplus)
}
# endif
#endif
/* Example Code...
__________________________________________*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define CT_PAGE 4096
#define CT_RAW_BUF ((CT_PAGE * 8) + CT_PAGE + 1)
#define CT_CACHELINE 128
int main(void)
{
unsigned char buf_raw[CT_RAW_BUF] = { '\0' };
struct region region;
rinit(®ion, buf_raw, CT_RAW_BUF);
unsigned char* buf_align = rallocex(®ion, CT_CACHELINE, CT_PAGE);
printf(
"buf_align:%p\n______________\n",
(void*)buf_align
);
for (unsigned int i = 0; i < 16; ++i)
{
unsigned char* buf_align_sub = rallocex(®ion, CT_CACHELINE,
CT_CACHELINE);
printf(
"buf_align_sub:%p\n",
(void*)buf_align_sub
);
}
//printf("\n\nFin!\n");
//fflush(stdout);
//getchar();
return 0;
}
_______________________
Can you get it to work?
Thanks. :^)
--- Synchronet 3.22a-Linux NewsLink 1.2