diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-10-22 14:10:47 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-10-22 14:16:15 -0700 |
| commit | 018294242819ed59e708e23cf40b822e843ec3ac (patch) | |
| tree | 83e0c114ed2e399e93f9177ba56107e653f6be3b /docs/ref/api/memory.md | |
| parent | 15d424be5ab7c446c33db184724d8a6a14938587 (diff) | |
| download | nng-018294242819ed59e708e23cf40b822e843ec3ac.tar.gz nng-018294242819ed59e708e23cf40b822e843ec3ac.tar.bz2 nng-018294242819ed59e708e23cf40b822e843ec3ac.zip | |
docs: add memory chapter
Diffstat (limited to 'docs/ref/api/memory.md')
| -rw-r--r-- | docs/ref/api/memory.md | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/docs/ref/api/memory.md b/docs/ref/api/memory.md new file mode 100644 index 00000000..02748123 --- /dev/null +++ b/docs/ref/api/memory.md @@ -0,0 +1,75 @@ +# Memory + +Managing {{i:memory}} and {{i:allocations}} is something that every C program has to deal with. +In the case of _NNG_, it can be more complicated because the underlying platform +code can provide different allocators that might not be compatible with the use +system allocator used by `malloc` and `free`. + +## Allocate Memory + +```c +void *nng_alloc(size_t size); +``` + +The {{i:`nng_alloc`}} function allocates a contiguous memory region of +at least _size_ bytes, and returns a pointer to it. +The memory will be 64-bit aligned. +Note that the memory may have random data in it, just like with `malloc`. + +If memory cannot be allocated for any reason, then `NULL` will be returned. +Applications that experience this should treat this like `NNG_ENOMEM`. + +Memory returned by `nng_alloc` can be used to hold message buffers, in which +case it can be directly passed to [`nng_send`][nng_send] using the flag `NNG_FLAG_ALLOC`. +Alternatively, it can be freed when no longer needed using [`nng_free`][nng_free]. + +> [!IMPORTANT] +> Do not use the system `free` function (or the C++ `delete` operator) to release this memory. +> On some configurations this may work, but on others it will lead to a crash or +> other unpredictable behavior. + +## Deallocate Memory + +```c +void nng_free(void *ptr, size_t size); +``` + +The {{i:`nng_free`}} function deallocates memory previously allocated by [`nng_alloc`][nng_alloc]. + +The _size_ argument must exactly match the _size_ argument that was supplied to +`nng_alloc` when the memory was allocated. + +## Duplicate String + +```c +char *nng_strdup(const char *src); +``` + +The {{i:`nng_strdup`}} duplicates the string _src_ and returns it. + +This is logically equivalent to using [`nng_alloc`][nng_alloc] +to allocate a region of memory of `[c] strlen(s) + 1` bytes, and then +using `strcpy` to copy the string into the destination before +returning it. + +The returned string should be deallocated with +[`nng_strfree`][nng_strfree], or may be deallocated using the +[`nng_free`][nng_free] using the length of the returned string plus +one (for the `NUL` terminating byte). + +## Free String + +```c +void nng_strfree(char *str); +``` + +The {{i:`nng_strfree`}} function is a convenience function that +can be used to deallocate strings allocated with [`nng_strdup`][nng_strdup]. + +It is effectively the same as `[c] nng_free(strlen(str) + 1)`. + +[nng_alloc]: #allocate-memory +[nng_free]: #deallocate-memory +[nng_strdup]: #duplicate-string +[nng_strfree]: #free-string +[nng_send]: TODO.md |
