🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #767

my old ralloc...

#767
From: Chris M. Thomasson
Date: Sun, 25 Oct 2026 21:14
269 lines
5871 bytes
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(&region, buf_raw, CT_RAW_BUF);

unsigned char* buf_align = rallocex(&region, 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(&region, 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. :^)
Technical Details
Message-ID: <11blreu$3vri7$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups: comp.lang.c
Subject: my old ralloc...
Date: Sun, 25 Oct 2026 14:14:06 -0700
Organization: A noiseless patient Spider
Lines: 269
Message-ID: <11blreu$3vri7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 25 Oct 2026 21:14:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="4189767"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19y6tH2tAn01zw9kVZT8oD0NeMNFEXbtlw="; posting-host="792908fec4aa45dbc14a04382b76f62a"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BKQ/y4BRnIQ3NenOQEZE7PcCEGo= sha256:LLoAN2wT709R7PpS7UrVcQCbQrAwZTCDk8HP2+DsJOc=
	sha1:I+t8YaUTh2XXlVHDpr8fyWFcJvA= sha256:gUP0Pc+PPKn6oCZahu3bQwEWQ3leu42POD1KQKcxK1w=
Content-Language: en-US