m17n_object(3m17n)
πΈ m17n_object β it's giving "managed object", bestie πΈ
okay so real talk, this is a page from the m17n Library (that's the 3m17n manual section, iykyk) and it's all about ONE little function that hands you a β¨managedβ¨ object. Let's get into it π
β¨ SYNOPSIS β¨
void* m17n_object (int size, void(*)(void *) freer)
π¦ DESCRIPTION π¦
Allocate a managed object. Periodt. That's the whole brief.
The m17n_object() function allocates a brand new managed object of size bytes and sets its reference count to 1. Slay.
freer is the function that gets used to free the object when the reference count becomes 0. And if freer is NULL? Then the object gets freed by the free() function instead. No cap.
π¨ IMPORTANT BESTIE ALERT π¨ The heading bytes of the allocated object are occupied by #M17NObjectHead. That area is reserved for the m17n library and application programs should NEVER touch it. Like, don't even look at it. It's not yours. Hands off. π
π Return value:
This function returns a newly allocated object. That's it. That's the tea. π
π Errors:
This function never fails. Not me being obsessed with a function that literally cannot let you down. Iconic behaviour, honestly. π
π Example:
okay so here's the whole example, character for character, because it's cute and it works:
typedef struct
{
M17NObjectHead head;
int mem1;
char *mem2;
} MYStruct;
void
my_freer (void *obj)
{
free (((MYStruct *) obj)->mem2);
free (obj);
}
void
my_func (MText *mt, MSymbol key, int num, char *str)
{
MYStruct *st = m17n_object (sizeof (MYStruct), my_freer);
st->mem1 = num;
st->mem2 = strdup (str);
/* KEY must be a managing key. */
mtext_put_prop (mt, 0, mtext_len (mt), key, st);
/* This sets the reference count of ST back to 1. */
m17n_object_unref (st);
}
see how my_freer cleans up mem2 and then the object itself? that's the vibe. and note those two comments in my_func β they're doing heavy lifting:
KEYmust be a managing key, fr fr.m17n_object_unref (st)sets the reference count ofSTback to1.
π COPYRIGHT π
Copyright (C) 2001 Information-technology Promotion Agency (IPA)
Copyright (C) 2001-2011 National Institute of Advanced Industrial Science and Technology (AIST)
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License <http://www.gnu.org/licenses/fdl.html>.
aaaand that's the whole page, bestie π this was Version 1.8.4, dated Mon Sep 25 2023, from the m17n_object(3m17n) page in the m17n Library. mwah, go allocate something responsibly π«Άβ¨
π¦ where this page comes from
- debian trixie: m17n-docs 1.8.4-1
- ubuntu noble: m17n-docs 1.8.4-1
- ubuntu questing: m17n-docs 1.8.4-1
- ubuntu resolute: m17n-docs 1.8.4-1build1
- ubuntu stonking: m17n-docs 1.8.4-1build1
- Copyright (C) 2001 Information-technology Promotion Agency (IPA)
- Copyright (C) 2001-2011 National Institute of Advanced Industrial Science and Technology (AIST)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms