Handmade Hero » Forums » Code » stb_truetype and STBTT_malloc/STBTT_free
Eric Fortier
2 posts
#10261 stb_truetype and STBTT_malloc/STBTT_free
1 year, 5 months ago

Hey all,

I just watched the episodes on fonts, and decided to implement Sean's truetype in my Delphi project. Everything is converting nicely, except that I don't seem to understand the alloc and free macros, defined and used as follow:

#define STBTT_malloc(x,u) ((void)(u),malloc(x))
#define STBTT_free(x,u) ((void)(u),free(x))

vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata);

I don't understand what the macro does with the info->userdata part when it's expanded. Could someone clarify this? How would this allocation be written without the macro, because I really don't get what its doing, unless its putting the pointer of the allocated memory in both vertices and info->userdata, which might be wrong.

Thanks for any help guys!
Mārtiņš Možeiko
1693 posts
1 project
#10262 stb_truetype and STBTT_malloc/STBTT_free
1 year, 5 months ago Edited by Mārtiņš Možeiko on Jan. 7, 2017, 6:57 p.m. Reason: typos

Remember that C preprocessor is dumb text replacement, it doesn't care about C semantics it just replaces one text with another. So that line expands to following C code:
vertices = (stbtt_vertex *) ((void)(info->userdata),malloc(m * sizeof(vertices[0])));

What does this mean?

C language has comma operator. if you write a,b,c then what compiler does is evaluate a, discards the result, then evaluate b, discards the result and then evaluate c which is the "result" of a,b,c expression.

For example:
x = (a = 2, c=3, b=a);

This is equivalent to writing following code:
a = 2;
x = (b=a); // or x=b

So back you the code line from your post, it actually means following:
vertices = (stbtt_vertex *) malloc(m * sizeof(vertices[0]));

First is harmless statement with no side effects that doesn't do anything, and second line is simple call to malloc which I think you understand.

So why do this kind of stuff with comma operator and userdata?
First of all - this comma operator is nice way how to discard unneeded expression, but still make compiler to evaluate it. Why to evaluate it? To avoid compiler warnings about unused variables. Imagine if code would be something like this:
int user = ...;
void* ptr = STBTT_malloc(100, user);

if STBTT_malloc would be only "#define STBTT_malloc(x) malloc(x)" then it compiler would complain that "int user" variable is never used. Which would be very annoying!

So why to do this thing with "u" userdata argument? It is there to support generic allocators. Sure malloc/free is very simple and requires only one argument, but you could imagine more universal allocator would need to support source from where to allocate memory (like arena/heap or something). So it could probably has following signature:
void* allocate(size_t size, void* memory_heap);
void deallocate(void* pointer, void* memory_heap);

This is actually very common thing in C - to add extra void* argument to callbacks that allows user to pass additional data to system and get it back in callback.

Here's simple example from Win32 API - EnumWindows. It enumerates all windows in system.
You call it by passing callback to function which will receive info about windows and extra argument (lParam), which will be passed back to your callback. Example usage:
BOOL CALLBACK MyCallback(HWND hwnd, LPARAM lParam)
    if (hwnd is window I'm looking for)
        HWND* result = (HWND*)lParam;
        *result = hwnd;
        return FALSE; // stop enumeration
    return TRUE; // continue enumeration


HWND result;
EnumWindows(&MyCallback, (LPARAM)&result); // this passes HWND* in lparam
// now result contains HWND that was written in callback

This makes code a bit cleaner so you don't need to use global variables which are written in one place and read in another place. It also helps to keep code thread-safe.
Eric Fortier
2 posts
#10263 stb_truetype and STBTT_malloc/STBTT_free
1 year, 5 months ago

Thank you for this great answer, Mārtiņš, I very much appreciate it and it explains everything I would ever think to ask!
Jeremiah Goerdt
198 posts
2 projects

Stay-at-home Dad, Programmer, and Linux apologist.

#10267 stb_truetype and STBTT_malloc/STBTT_free
1 year, 5 months ago

We need some sort of reputation or upvoting on these forums. You're a bamf mmozeiko

Build a man a fire, he'll be warm for a day.
Set a man on fire, he'll be warm for the rest of his life.