summaryrefslogtreecommitdiff
path: root/doc/libstddjb/alloc.html
blob: 646a206476abec11c584329227f33b95f9db0625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the alloc library interface</title>
    <meta name="Description" content="skalibs: the alloc library interface" />
    <meta name="Keywords" content="skalibs c unix alloc library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>alloc</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/alloc.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>alloc</tt> is the skalibs heap memory manager. It's actually a
wrapper for the
<a href="https://www.opengroup.org/onlinepubs/9699919799/functions/malloc.html">malloc()</a>
series of functions; it unifies a few system-dependent <tt>malloc</tt>
behaviours. It's also the API to implement and preload if for some
reason you need to plug in your own allocator: replacing <tt>alloc()</tt>
is much easier than replacing <tt>malloc()</tt> safely.
</p>

<p>
<strong> As a general rule, you should not be using the <tt>alloc</tt>
interface directly. </strong> Allocating and freeing individual cells
in the heap is a recipe for heap fragmentation, as well as cell
tracking nightmares leading to memory leaks. <strong> You should use
the higher-level <a href="stralloc.html">stralloc</a> and
<a href="genalloc.html">genalloc</a> interfaces </strong> to handle dynamic
arrays of objects.
</p>

<p>
 C's lack of automatic management of heap memory is not a drawback: it's
a feature of the language. It allows for code that is one or two orders
of magnitude faster than the equivalent in a higher-level language,
and very low on resources consumption. However, it requires more attention
from the programmer. Good APIs can significantly reduce the difficulty of
keeping track of every heap-allocated cell, and every smart programmer
should favor them over basic interfaces like <tt>malloc()</tt>.
</p>

<p>
 <tt>alloc</tt> is used internally by skalibs to implement
<a href="stralloc.html">stralloc</a>, and nowhere else.
</p>

<h2> Functions </h2>

<p>
<code> void *alloc (size_t len) </code> <br />
Allocates a block of <em>len</em> bytes in the heap and returns a pointer
to the start of the block (or NULL if it failed).
If <em>len</em> is 0, the function returns a unique pointer that
cannot be written to, but that is <em>not null</em>. Note that this is
different from the required C99 behaviour for <tt>malloc()</tt>.
</p>

<p>
<code> void alloc_free (void *p) </code> <br />
Frees the block of heap memory pointed to by <em>p</em>.
</p>

<p>
<code> int alloc_realloc (char **p, size_t newlen) </code> <br />
Redimension the block of heap memory pointed to by *<em>p</em> to
<em>newlen</em> bytes. The block may have to be moved, in which case
*<em>p</em> will be modified. Normally returns 1; if an error occurred,
returns 0 and sets errno, and neither *<em>p</em> nor its contents are
modified. Note that <em>p</em> must be a pointer to a <tt>char *</tt>,
because polymorphism isn't possible here (for reasons that have to do
with pointer representation in C).
</p>

<p>
<code> int alloc_re (char **p, size_t oldlen, size_t newlen) </code> <br />
Legacy interface for reallocation. It works like <tt>alloc_realloc</tt>,
except that the original block length must be provided as the <em>oldlen</em>
argument.
</p>

</body>
</html>