Article View: comp.lang.c
Article #352Re: AVL tree insertion - programming exercise
From: Chris M. Thomasson
Date: Tue, 13 Oct 2026 20:16
Date: Tue, 13 Oct 2026 20:16
238 lines
6999 bytes
6999 bytes
On 10/13/2026 2:42 AM, David Brown wrote:
> On 13/10/2026 10:38, chine.bleu wrote:
>> Tim Rentsch wrote:
>>> sons[2] array. These links are basically pointers, but held
>>> as uintptr_t so they can take one of the bits to mean "tall",
>>> which means taller than the other subtree of the node. The
>>> tree structure should maintain an invariant that at most one
>>
>> Memory is cheap.
>
> Not any more!
>
>> Do not encode tags into low bits of an address unless you really know
>> what your doing.
>
> Agreed, as a principle.
>
>> As a precaution check the return address of malloc
>>
>> p = malloc(m);
>> if ((uintptr_t)p)&1!=0) abort();
>>
>
> malloc always returns an address with suitable alignment for any
> fundamental alignment on the target. Typically that means at least 64-
> bit alignment (and thus the three LSB's are always zero), but it is
> implementation dependent.
>
Right, and sometimes that can be wasteful on certain memory constrained
environments. Fwiw, I wrote a region allocator a while back that tends
to pack thing on their alignment, but never needs a max a alignment for
all.
Oh shit! I cannot get into google groups, I get a god damn: 429 Too Many
Requests
Well, here is a mutated version of it that uses C++. I had the C only
version in some of my saved google groups links. Sigh. Anyway:
My code:
____________________________________________
#if ! defined (RALLOC_H)
# define RALLOC_H
# if defined (__cplusplus)
extern "C" {
# endif
/**************************************************************/
#include <cstddef>
#include <cassert>
#if defined (_MSC_VER)
/* warning C4116: unnamed type definition in parentheses */
# pragma warning (disable : 4116)
#endif
#if ! defined (NDEBUG)
# include <cstdio>
# define RALLOC_DBG_PRINTF(mp_exp) std::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
];
#define RALLOC_ALIGN_OF(mp_type) alignof(mp_type)
#define RALLOC_ALIGN_MAX alignof(std::max_align_t)
#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 = (unsigned char*)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 = (unsigned char*)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 = %llu\n"
" alignment = %llu\n"
" origin offset = %llu\n"
" final offset = %llu\n"
" raw_buffer = %p\n"
" align_buffer = %p\n"
" size adjustment = %llu\n"
" final size = %llu\n"
"}\n\n\n",
(void*)self,
(unsigned long long int)size - (align_buffer - raw_buffer),
(unsigned long long int)align,
(unsigned long long int)offset,
(unsigned long long int)self->offset,
(void*)raw_buffer,
(void*)align_buffer,
(unsigned long long int)(align_buffer - raw_buffer),
(unsigned long 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
Technical Details
Message-ID:
<11am3jf$sjl5$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-mailReferences:
<86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me>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: Re: AVL tree insertion - programming exercise Date: Tue, 13 Oct 2026 13:16:44 -0700 Organization: A noiseless patient Spider Lines: 238 Message-ID: <11am3jf$sjl5$1@dont-email.me> References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 13 Oct 2026 20:16:47 +0000 (UTC) Injection-Info: dont-email.me; logging-data="937637"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/B8Udty5BD2t8/Z19RyEvqGNHlWllSDek="; posting-host="094496f6b743c51c3f22a7000b0b8b16" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:e96553kBPyK+5NlUBoh9+99KDtU= sha256:w/l59osHVNVPEVTr9n+hBB+X2NulpipTVdePoz8tBXU= sha1:DqLQu6m8bk/vMaOHZR9VRLnwl2U= sha256:+8rmL642TSQiPGOKDG6z3Byy13hkleCIfRMtLwf1DSs= Content-Language: en-US In-Reply-To: <11akuf1$bq6c$1@dont-email.me>