source: doc/theses/mubeen_zulfiqar_MMath/allocator.tex @ ba897d21

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since ba897d21 was ba897d21, checked in by m3zulfiq <m3zulfiq@…>, 2 years ago

added benchmark and evaluations chapter to thesis

  • Property mode set to 100644
File size: 35.2 KB
Line 
1\chapter{Allocator}
2
3\section{uHeap}
4uHeap is a lightweight memory allocator. The objective behind uHeap is to design a minimal concurrent memory allocator that has new features and also fulfills GNU C Library requirements (FIX ME: cite requirements).
5
6The objective of uHeap's new design was to fulfill following requirements:
7\begin{itemize}
8\item It should be concurrent and thread-safe for multi-threaded programs.
9\item It should avoid global locks, on resources shared across all threads, as much as possible.
10\item It's performance (FIX ME: cite performance benchmarks) should be comparable to the commonly used allocators (FIX ME: cite common allocators).
11\item It should be a lightweight memory allocator.
12\end{itemize}
13
14%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15
16\section{Design choices for uHeap}\label{sec:allocatorSec}
17uHeap's design was reviewed and changed to fulfill new requirements (FIX ME: cite allocator philosophy). For this purpose, following two designs of uHeapLmm were proposed:
18
19\paragraph{Design 1: Centralized}
20One heap, but lower bucket sizes are N-shared across KTs.
21This design leverages the fact that 95\% of allocation requests are less than 512 bytes and there are only 3--5 different request sizes.
22When KTs $\le$ N, the important bucket sizes are uncontented.
23When KTs $>$ N, the free buckets are contented.
24Therefore, threads are only contending for a small number of buckets, which are distributed among them to reduce contention.
25\begin{cquote}
26\centering
27\input{AllocDS2}
28\end{cquote}
29Problems: need to know when a kernel thread (KT) is created and destroyed to know when to assign a shared bucket-number.
30When no thread is assigned a bucket number, its free storage is unavailable. All KTs will be contended for one lock on sbrk for their initial allocations (before free-lists gets populated).
31
32\paragraph{Design 2: Decentralized N Heaps}
33Fixed number of heaps: shard the heap into N heaps each with a bump-area allocated from the @sbrk@ area.
34Kernel threads (KT) are assigned to the N heaps.
35When KTs $\le$ N, the heaps are uncontented.
36When KTs $>$ N, the heaps are contented.
37By adjusting N, this approach reduces storage at the cost of speed due to contention.
38In all cases, a thread acquires/releases a lock, contented or uncontented.
39\begin{cquote}
40\centering
41\input{AllocDS1}
42\end{cquote}
43Problems: need to know when a KT is created and destroyed to know when to assign/un-assign a heap to the KT.
44
45\paragraph{Design 3: Decentralized Per-thread Heaps}
46Design 3 is similar to design 2 but instead of having an M:N model, it uses a 1:1 model. So, instead of having N heaos and sharing them among M KTs, Design 3 has one heap for each KT.
47Dynamic number of heaps: create a thread-local heap for each kernel thread (KT) with a bump-area allocated from the @sbrk@ area.
48Each KT will have its own exclusive thread-local heap. Heap will be uncontended between KTs regardless how many KTs have been created.
49Operations on @sbrk@ area will still be protected by locks.
50%\begin{cquote}
51%\centering
52%\input{AllocDS3} FIXME add figs
53%\end{cquote}
54Problems: We cannot destroy the heap when a KT exits because our dynamic objects have ownership and they are returned to the heap that created them when the program frees a dynamic object. All dynamic objects point back to their owner heap. If a thread A creates an object O, passes it to another thread B, and A itself exits. When B will free object O, O should return to A's heap so A's heap should be preserved for the lifetime of the whole program as their might be objects in-use of other threads that were allocated by A. Also, we need to know when a KT is created and destroyed to know when to create/destroy a heap for the KT.
55
56\paragraph{Design 4: Decentralized Per-CPU Heaps}
57Design 4 is similar to Design 3 but instead of having a heap for each thread, it creates a heap for each CPU.
58Fixed number of heaps for a machine: create a heap for each CPU with a bump-area allocated from the @sbrk@ area.
59Each CPU will have its own CPU-local heap. When the program does a dynamic memory operation, it will be entertained by the heap of the CPU where the process is currently running on.
60Each CPU will have its own exclusive heap. Just like Design 3(FIXME cite), heap will be uncontended between KTs regardless how many KTs have been created.
61Operations on @sbrk@ area will still be protected by locks.
62To deal with preemtion during a dynamic memory operation, librseq(FIXME cite) will be used to make sure that the whole dynamic memory operation completes on one CPU. librseq's restartable sequences can make it possible to re-run a critical section and undo the current writes if a preemption happened during the critical section's execution.
63%\begin{cquote}
64%\centering
65%\input{AllocDS4} FIXME add figs
66%\end{cquote}
67
68Problems: This approach was slower than the per-thread model. Also, librseq does not provide such restartable sequences to detect preemtions in user-level threading system which is important to us as CFA(FIXME cite) has its own threading system that we want to support.
69
70Out of the four designs, Design 3 was chosen because of the following reasons.
71\begin{itemize}
72\item
73Decentralized designes are better in general as compared to centralized design because their concurrency is better across all bucket-sizes as design 1 shards a few buckets of selected sizes while other designs shards all the buckets. Decentralized designes shard the whole heap which has all the buckets with the addition of sharding sbrk area. So Design 1 was eliminated.
74\item
75Design 2 was eliminated because it has a possibility of contention in-case of KT > N while Design 3 and 4 have no contention in any scenerio.
76\item
77Design 4 was eliminated because it was slower than Design 3 and it provided no way to achieve user-threading safety using librseq. We had to use CFA interruption handling to achive user-threading safety which has some cost to it. Desing 4 was already slower than Design 3, adding cost of interruption handling on top of that would have made it even slower.
78\end{itemize}
79
80
81\subsection{Advantages of distributed design}
82
83The distributed design of uHeap is concurrent to work in multi-threaded applications.
84
85Some key benefits of the distributed design of uHeap are as follows:
86
87\begin{itemize}
88\item
89The bump allocation is concurrent as memory taken from sbrk is sharded across all heaps as bump allocation reserve. The call to sbrk will be protected using locks but bump allocation (on memory taken from sbrk) will not be contended once the sbrk call has returned.
90\item
91Low or almost no contention on heap resources.
92\item
93It is possible to use sharing and stealing techniques to share/find unused storage, when a free list is unused or empty.
94\item
95Distributed design avoids unnecassry locks on resources shared across all KTs.
96\end{itemize}
97
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
100\section{uHeap Structure}
101
102As described in (FIXME cite 2.4) uHeap uses following features of multi-threaded memory allocators.
103\begin{itemize}
104\item
105uHeap has multiple heaps without a global heap and uses 1:1 model. (FIXME cite 2.5 1:1 model)
106\item
107uHeap uses object ownership. (FIXME cite 2.5.2)
108\item
109uHeap does not use object containers (FIXME cite 2.6) or any coalescing technique. Instead each dynamic object allocated by uHeap has a header than contains bookkeeping information.
110\item
111Each thread-local heap in uHeap has its own allocation buffer that is taken from the system using sbrk() call. (FIXME cite 2.7)
112\item
113Unless a heap is freeing an object that is owned by another thread's heap or heap is using sbrk() system call, uHeap is mostly lock-free which eliminates most of the contention on shared resources. (FIXME cite 2.8)
114\end{itemize}
115
116As uHeap uses a heap per-thread model to reduce contention on heap resources, we manage a list of heaps (heap-list) that can be used by threads. The list is empty at the start of the program. When a kernel thread (KT) is created, we check if heap-list is empty. If no then a heap is removed from the heap-list and is given to this new KT to use exclusively. If yes then a new heap object is created in dynamic memory and is given to this new KT to use exclusively. When a KT exits, its heap is not destroyed but instead its heap is put on the heap-list and is ready to be reused by new KTs.
117
118This reduces the memory footprint as the objects on free-lists of a KT that has exited can be reused by a new KT. Also, we preserve all the heaps that were created during the lifetime of the program till the end of the program. uHeap uses object ownership where an object is freed to the free-buckets of the heap that allocated it. Even after a KT A has exited, its heap has to be preserved as there might be objects in-use of other threads that were initially allocated by A and the passed to other threads.
119
120\begin{figure}
121\centering
122\includegraphics[width=0.65\textwidth]{figures/NewHeapStructure.eps}
123\caption{uHeap Structure}
124\label{fig:heapStructureFig}
125\end{figure}
126
127Each heap uses seggregated free-buckets that have free objects of a specific size. Each free-bucket of a specific size has following 2 lists in it:
128\begin{itemize}
129\item
130Free list is used when a thread is freeing an object that is owned by its own heap so free list does not use any locks/atomic-operations as it is only used by the owner KT.
131\item
132Away list is used when a thread A is freeing an object that is owned by another KT B's heap. This object should be freed to the owner heap (B's heap) so A will place the object on the away list of B. Away list is lock protected as it is shared by all other threads.
133\end{itemize}
134
135When a dynamic object of a size S is requested. The thread-local heap will check if S is greater than or equal to the mmap threshhold. Any request larger than the mmap threshhold is fulfilled by allocating an mmap area of that size and such requests are not allocated on sbrk area. The value of this threshhold can be changed using mallopt routine but the new value should not be larger than our biggest free-bucket size.
136
137Algorithm~\ref{alg:heapObjectAlloc} briefly shows how an allocation request is fulfilled.
138
139\begin{algorithm}
140\caption{Dynamic object allocation of size S}\label{alg:heapObjectAlloc}
141\begin{algorithmic}[1]
142\State $\textit{O} \gets \text{NULL}$
143\If {$S < \textit{mmap-threshhold}$}
144        \State $\textit{B} \gets (\text{smallest free-bucket} \geq S)$
145        \If {$\textit{B's free-list is empty}$}
146                \If {$\textit{B's away-list is empty}$}
147                        \If {$\textit{heap's allocation buffer} < S$}
148                                \State $\text{get allocation buffer using system call sbrk()}$
149                        \EndIf
150                        \State $\textit{O} \gets \text{bump allocate an object of size S from allocation buffer}$
151                \Else
152                        \State $\textit{merge B's away-list into free-list}$
153                        \State $\textit{O} \gets \text{pop an object from B's free-list}$
154                \EndIf
155        \Else
156                \State $\textit{O} \gets \text{pop an object from B's free-list}$
157        \EndIf
158        \State $\textit{O's owner} \gets \text{B}$
159\Else
160        \State $\textit{O} \gets \text{allocate dynamic memory using system call mmap with size S}$
161\EndIf
162\State $\Return \textit{ O}$
163\end{algorithmic}
164\end{algorithm}
165
166Algorithm~\ref{alg:heapObjectFreeOwn} shows how a free request is fulfilled if object ownership is turned on. Algorithm~\ref{alg:heapObjectFreeNoOwn} shows how the same free request is fulfilled without object ownership.
167
168\begin{algorithm}
169\caption{Dynamic object free at address A with object ownership}\label{alg:heapObjectFreeOwn}
170\begin{algorithmic}[1]
171\If {$\textit{A was mmap-ed}$}
172        \State $\text{return A's dynamic memory to system using system call munmap}$
173\Else
174        \State $\text{B} \gets \textit{O's owner}$
175        \If {$\textit{B is thread-local heap's bucket}$}
176                \State $\text{push A to B's free-list}$
177        \Else
178                \State $\text{push A to B's away-list}$
179        \EndIf
180\EndIf
181\end{algorithmic}
182\end{algorithm}
183
184\begin{algorithm}
185\caption{Dynamic object free at address A without object ownership}\label{alg:heapObjectFreeNoOwn}
186\begin{algorithmic}[1]
187\If {$\textit{A was mmap-ed}$}
188        \State $\text{return A's dynamic memory to system using system call munmap}$
189\Else
190        \State $\text{B} \gets \textit{O's owner}$
191        \If {$\textit{B is thread-local heap's bucket}$}
192                \State $\text{push A to B's free-list}$
193        \Else
194                \State $\text{C} \gets \textit{thread local heap's bucket with same size as B}$
195                \State $\text{push A to C's free-list}$
196        \EndIf
197\EndIf
198\end{algorithmic}
199\end{algorithm}
200
201
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203
204\section{Added Features and Methods}
205To improve the uHeap allocator (FIX ME: cite uHeap) interface and make it more user friendly, we added a few more routines to the C allocator. Also, we built a \CFA (FIX ME: cite cforall) interface on top of C interface to increase the usability of the allocator.
206
207\subsection{C Interface}
208We added a few more features and routines to the allocator's C interface that can make the allocator more usable to the programmers. THese features will programmer more control on the dynamic memory allocation.
209
210\subsection{Out of Memory}
211
212Most allocators use @nullptr@ to indicate an allocation failure, specifically out of memory;
213hence the need to return an alternate value for a zero-sized allocation.
214The alternative is to abort a program when out of memory.
215In theory, notifying the programmer allows recovery;
216in practice, it is almost impossible to gracefully when out of memory, so the cheaper approach of returning @nullptr@ for a zero-sized allocation is chosen.
217
218
219\subsection{\lstinline{void * aalloc( size_t dim, size_t elemSize )}}
220@aalloc@ is an extension of malloc. It allows programmer to allocate a dynamic array of objects without calculating the total size of array explicitly. The only alternate of this routine in the other allocators is calloc but calloc also fills the dynamic memory with 0 which makes it slower for a programmer who only wants to dynamically allocate an array of objects without filling it with 0.
221\paragraph{Usage}
222@aalloc@ takes two parameters.
223
224\begin{itemize}
225\item
226@dim@: number of objects in the array
227\item
228@elemSize@: size of the object in the array.
229\end{itemize}
230It returns address of dynamic object allocatoed on heap that can contain dim number of objects of the size elemSize. On failure, it returns a @NULL@ pointer.
231
232\subsection{\lstinline{void * resize( void * oaddr, size_t size )}}
233@resize@ is an extension of relloc. It allows programmer to reuse a cuurently allocated dynamic object with a new size requirement. Its alternate in the other allocators is @realloc@ but relloc also copy the data in old object to the new object which makes it slower for the programmer who only wants to reuse an old dynamic object for a new size requirement but does not want to preserve the data in the old object to the new object.
234\paragraph{Usage}
235@resize@ takes two parameters.
236
237\begin{itemize}
238\item
239@oaddr@: the address of the old object that needs to be resized.
240\item
241@size@: the new size requirement of the to which the old object needs to be resized.
242\end{itemize}
243It returns an object that is of the size given but it does not preserve the data in the old object. On failure, it returns a @NULL@ pointer.
244
245\subsection{\lstinline{void * resize( void * oaddr, size_t nalign, size_t size )}}
246This @resize@ is an extension of the above @resize@ (FIX ME: cite above resize). In addition to resizing the size of of an old object, it can also realign the old object to a new alignment requirement.
247\paragraph{Usage}
248This resize takes three parameters. It takes an additional parameter of nalign as compared to the above resize (FIX ME: cite above resize).
249
250\begin{itemize}
251\item
252@oaddr@: the address of the old object that needs to be resized.
253\item
254@nalign@: the new alignment to which the old object needs to be realigned.
255\item
256@size@: the new size requirement of the to which the old object needs to be resized.
257\end{itemize}
258It returns an object with the size and alignment given in the parameters. On failure, it returns a @NULL@ pointer.
259
260\subsection{\lstinline{void * amemalign( size_t alignment, size_t dim, size_t elemSize )}}
261amemalign is a hybrid of memalign and aalloc. It allows programmer to allocate an aligned dynamic array of objects without calculating the total size of the array explicitly. It frees the programmer from calculating the total size of the array.
262\paragraph{Usage}
263amemalign takes three parameters.
264
265\begin{itemize}
266\item
267@alignment@: the alignment to which the dynamic array needs to be aligned.
268\item
269@dim@: number of objects in the array
270\item
271@elemSize@: size of the object in the array.
272\end{itemize}
273It returns a dynamic array of objects that has the capacity to contain dim number of objects of the size of elemSize. The returned dynamic array is aligned to the given alignment. On failure, it returns a @NULL@ pointer.
274
275\subsection{\lstinline{void * cmemalign( size_t alignment, size_t dim, size_t elemSize )}}
276cmemalign is a hybrid of amemalign and calloc. It allows programmer to allocate an aligned dynamic array of objects that is 0 filled. The current way to do this in other allocators is to allocate an aligned object with memalign and then fill it with 0 explicitly. This routine provides both features of aligning and 0 filling, implicitly.
277\paragraph{Usage}
278cmemalign takes three parameters.
279
280\begin{itemize}
281\item
282@alignment@: the alignment to which the dynamic array needs to be aligned.
283\item
284@dim@: number of objects in the array
285\item
286@elemSize@: size of the object in the array.
287\end{itemize}
288It returns a dynamic array of objects that has the capacity to contain dim number of objects of the size of elemSize. The returned dynamic array is aligned to the given alignment and is 0 filled. On failure, it returns a @NULL@ pointer.
289
290\subsection{\lstinline{size_t malloc_alignment( void * addr )}}
291@malloc_alignment@ returns the alignment of a currently allocated dynamic object. It allows the programmer in memory management and personal bookkeeping. It helps the programmer in verofying the alignment of a dynamic object especially in a scenerio similar to prudcer-consumer where a producer allocates a dynamic object and the consumer needs to assure that the dynamic object was allocated with the required alignment.
292\paragraph{Usage}
293@malloc_alignment@ takes one parameters.
294
295\begin{itemize}
296\item
297@addr@: the address of the currently allocated dynamic object.
298\end{itemize}
299@malloc_alignment@ returns the alignment of the given dynamic object. On failure, it return the value of default alignment of the uHeap allocator.
300
301\subsection{\lstinline{bool malloc_zero_fill( void * addr )}}
302@malloc_zero_fill@ returns whether a currently allocated dynamic object was initially zero filled at the time of allocation. It allows the programmer in memory management and personal bookkeeping. It helps the programmer in verifying the zero filled property of a dynamic object especially in a scenerio similar to prudcer-consumer where a producer allocates a dynamic object and the consumer needs to assure that the dynamic object was zero filled at the time of allocation.
303\paragraph{Usage}
304@malloc_zero_fill@ takes one parameters.
305
306\begin{itemize}
307\item
308@addr@: the address of the currently allocated dynamic object.
309\end{itemize}
310@malloc_zero_fill@ returns true if the dynamic object was initially zero filled and return false otherwise. On failure, it returns false.
311
312\subsection{\lstinline{size_t malloc_size( void * addr )}}
313@malloc_size@ returns the allocation size of a currently allocated dynamic object. It allows the programmer in memory management and personal bookkeeping. It helps the programmer in verofying the alignment of a dynamic object especially in a scenerio similar to prudcer-consumer where a producer allocates a dynamic object and the consumer needs to assure that the dynamic object was allocated with the required size. Its current alternate in the other allocators is @malloc_usable_size@. But, @malloc_size@ is different from @malloc_usable_size@ as @malloc_usabe_size@ returns the total data capacity of dynamic object including the extra space at the end of the dynamic object. On the other hand, @malloc_size@ returns the size that was given to the allocator at the allocation of the dynamic object. This size is updated when an object is realloced, resized, or passed through a similar allocator routine.
314\paragraph{Usage}
315@malloc_size@ takes one parameters.
316
317\begin{itemize}
318\item
319@addr@: the address of the currently allocated dynamic object.
320\end{itemize}
321@malloc_size@ returns the allocation size of the given dynamic object. On failure, it return zero.
322
323\subsection{\lstinline{void * realloc( void * oaddr, size_t nalign, size_t size )}}
324This @realloc@ is an extension of the default @realloc@ (FIX ME: cite default @realloc@). In addition to reallocating an old object and preserving the data in old object, it can also realign the old object to a new alignment requirement.
325\paragraph{Usage}
326This @realloc@ takes three parameters. It takes an additional parameter of nalign as compared to the default @realloc@.
327
328\begin{itemize}
329\item
330@oaddr@: the address of the old object that needs to be reallocated.
331\item
332@nalign@: the new alignment to which the old object needs to be realigned.
333\item
334@size@: the new size requirement of the to which the old object needs to be resized.
335\end{itemize}
336It returns an object with the size and alignment given in the parameters that preserves the data in the old object. On failure, it returns a @NULL@ pointer.
337
338\subsection{\CFA Malloc Interface}
339We added some routines to the malloc interface of \CFA. These routines can only be used in \CFA and not in our standalone uHeap allocator as these routines use some features that are only provided by \CFA and not by C. It makes the allocator even more usable to the programmers.
340\CFA provides the liberty to know the returned type of a call to the allocator. So, mainly in these added routines, we removed the object size parameter from the routine as allocator can calculate the size of the object from the returned type.
341
342\subsection{\lstinline{T * malloc( void )}}
343This malloc is a simplified polymorphic form of defualt malloc (FIX ME: cite malloc). It does not take any parameter as compared to default malloc that takes one parameter.
344\paragraph{Usage}
345This malloc takes no parameters.
346It returns a dynamic object of the size of type @T@. On failure, it returns a @NULL@ pointer.
347
348\subsection{\lstinline{T * aalloc( size_t dim )}}
349This aalloc is a simplified polymorphic form of above aalloc (FIX ME: cite aalloc). It takes one parameter as compared to the above aalloc that takes two parameters.
350\paragraph{Usage}
351aalloc takes one parameters.
352
353\begin{itemize}
354\item
355@dim@: required number of objects in the array.
356\end{itemize}
357It returns a dynamic object that has the capacity to contain dim number of objects, each of the size of type @T@. On failure, it returns a @NULL@ pointer.
358
359\subsection{\lstinline{T * calloc( size_t dim )}}
360This calloc is a simplified polymorphic form of defualt calloc (FIX ME: cite calloc). It takes one parameter as compared to the default calloc that takes two parameters.
361\paragraph{Usage}
362This calloc takes one parameter.
363
364\begin{itemize}
365\item
366@dim@: required number of objects in the array.
367\end{itemize}
368It returns a dynamic object that has the capacity to contain dim number of objects, each of the size of type @T@. On failure, it returns a @NULL@ pointer.
369
370\subsection{\lstinline{T * resize( T * ptr, size_t size )}}
371This resize is a simplified polymorphic form of above resize (FIX ME: cite resize with alignment). It takes two parameters as compared to the above resize that takes three parameters. It frees the programmer from explicitly mentioning the alignment of the allocation as \CFA provides gives allocator the liberty to get the alignment of the returned type.
372\paragraph{Usage}
373This resize takes two parameters.
374
375\begin{itemize}
376\item
377@ptr@: address of the old object.
378\item
379@size@: the required size of the new object.
380\end{itemize}
381It returns a dynamic object of the size given in paramters. The returned object is aligned to the alignemtn of type @T@. On failure, it returns a @NULL@ pointer.
382
383\subsection{\lstinline{T * realloc( T * ptr, size_t size )}}
384This @realloc@ is a simplified polymorphic form of defualt @realloc@ (FIX ME: cite @realloc@ with align). It takes two parameters as compared to the above @realloc@ that takes three parameters. It frees the programmer from explicitly mentioning the alignment of the allocation as \CFA provides gives allocator the liberty to get the alignment of the returned type.
385\paragraph{Usage}
386This @realloc@ takes two parameters.
387
388\begin{itemize}
389\item
390@ptr@: address of the old object.
391\item
392@size@: the required size of the new object.
393\end{itemize}
394It returns a dynamic object of the size given in paramters that preserves the data in the given object. The returned object is aligned to the alignemtn of type @T@. On failure, it returns a @NULL@ pointer.
395
396\subsection{\lstinline{T * memalign( size_t align )}}
397This memalign is a simplified polymorphic form of defualt memalign (FIX ME: cite memalign). It takes one parameters as compared to the default memalign that takes two parameters.
398\paragraph{Usage}
399memalign takes one parameters.
400
401\begin{itemize}
402\item
403@align@: the required alignment of the dynamic object.
404\end{itemize}
405It returns a dynamic object of the size of type @T@ that is aligned to given parameter align. On failure, it returns a @NULL@ pointer.
406
407\subsection{\lstinline{T * amemalign( size_t align, size_t dim )}}
408This amemalign is a simplified polymorphic form of above amemalign (FIX ME: cite amemalign). It takes two parameter as compared to the above amemalign that takes three parameters.
409\paragraph{Usage}
410amemalign takes two parameters.
411
412\begin{itemize}
413\item
414@align@: required alignment of the dynamic array.
415\item
416@dim@: required number of objects in the array.
417\end{itemize}
418It returns a dynamic object that has the capacity to contain dim number of objects, each of the size of type @T@. The returned object is aligned to the given parameter align. On failure, it returns a @NULL@ pointer.
419
420\subsection{\lstinline{T * cmemalign( size_t align, size_t dim  )}}
421This cmemalign is a simplified polymorphic form of above cmemalign (FIX ME: cite cmemalign). It takes two parameter as compared to the above cmemalign that takes three parameters.
422\paragraph{Usage}
423cmemalign takes two parameters.
424
425\begin{itemize}
426\item
427@align@: required alignment of the dynamic array.
428\item
429@dim@: required number of objects in the array.
430\end{itemize}
431It returns a dynamic object that has the capacity to contain dim number of objects, each of the size of type @T@. The returned object is aligned to the given parameter align and is zero filled. On failure, it returns a @NULL@ pointer.
432
433\subsection{\lstinline{T * aligned_alloc( size_t align )}}
434This @aligned_alloc@ is a simplified polymorphic form of defualt @aligned_alloc@ (FIX ME: cite @aligned_alloc@). It takes one parameter as compared to the default @aligned_alloc@ that takes two parameters.
435\paragraph{Usage}
436This @aligned_alloc@ takes one parameter.
437
438\begin{itemize}
439\item
440@align@: required alignment of the dynamic object.
441\end{itemize}
442It returns a dynamic object of the size of type @T@ that is aligned to the given parameter. On failure, it returns a @NULL@ pointer.
443
444\subsection{\lstinline{int posix_memalign( T ** ptr, size_t align )}}
445This @posix_memalign@ is a simplified polymorphic form of defualt @posix_memalign@ (FIX ME: cite @posix_memalign@). It takes two parameters as compared to the default @posix_memalign@ that takes three parameters.
446\paragraph{Usage}
447This @posix_memalign@ takes two parameter.
448
449\begin{itemize}
450\item
451@ptr@: variable address to store the address of the allocated object.
452\item
453@align@: required alignment of the dynamic object.
454\end{itemize}
455
456It stores address of the dynamic object of the size of type @T@ in given parameter ptr. This object is aligned to the given parameter. On failure, it returns a @NULL@ pointer.
457
458\subsection{\lstinline{T * valloc( void )}}
459This @valloc@ is a simplified polymorphic form of defualt @valloc@ (FIX ME: cite @valloc@). It takes no parameters as compared to the default @valloc@ that takes one parameter.
460\paragraph{Usage}
461@valloc@ takes no parameters.
462It returns a dynamic object of the size of type @T@ that is aligned to the page size. On failure, it returns a @NULL@ pointer.
463
464\subsection{\lstinline{T * pvalloc( void )}}
465\paragraph{Usage}
466@pvalloc@ takes no parameters.
467It returns a dynamic object of the size that is calcutaed by rouding the size of type @T@. The returned object is also aligned to the page size. On failure, it returns a @NULL@ pointer.
468
469\subsection{Alloc Interface}
470In addition to improve allocator interface both for \CFA and our standalone allocator uHeap in C. We also added a new alloc interface in \CFA that increases usability of dynamic memory allocation.
471This interface helps programmers in three major ways.
472
473\begin{itemize}
474\item
475Routine Name: alloc interfce frees programmers from remmebring different routine names for different kind of dynamic allocations.
476\item
477Parametre Positions: alloc interface frees programmers from remembering parameter postions in call to routines.
478\item
479Object Size: alloc interface does not require programmer to mention the object size as \CFA allows allocator to determince the object size from returned type of alloc call.
480\end{itemize}
481
482Alloc interface uses polymorphism, backtick routines (FIX ME: cite backtick) and ttype parameters of \CFA (FIX ME: cite ttype) to provide a very simple dynamic memory allocation interface to the programmers. The new interfece has just one routine name alloc that can be used to perform a wide range of dynamic allocations. The parameters use backtick functions to provide a similar-to named parameters feature for our alloc interface so that programmers do not have to remember parameter positions in alloc call except the position of dimension (dim) parameter.
483
484\subsection{Routine: \lstinline{T * alloc( ... )}}
485Call to alloc wihout any parameter returns one object of size of type @T@ allocated dynamically.
486Only the dimension (dim) parameter for array allocation has the fixed position in the alloc routine. If programmer wants to allocate an array of objects that the required number of members in the array has to be given as the first parameter to the alloc routine.
487alocc routine accepts six kinds of arguments. Using different combinations of tha parameters, different kind of allocations can be performed. Any combincation of parameters can be used together except @`realloc@ and @`resize@ that should not be used simultanously in one call to routine as it creates ambiguity about whether to reallocate or resize a currently allocated dynamic object. If both @`resize@ and @`realloc@ are used in a call to alloc then the latter one will take effect or unexpected resulted might be produced.
488
489\paragraph{Dim}
490This is the only parameter in the alloc routine that has a fixed-position and it is also the only parameter that does not use a backtick function. It has to be passed at the first position to alloc call in-case of an array allocation of objects of type @T@.
491It represents the required number of members in the array allocation as in \CFA's aalloc (FIX ME: cite aalloc).
492This parameter should be of type @size_t@.
493
494Example: @int a = alloc( 5 )@
495This call will return a dynamic array of five integers.
496
497\paragraph{Align}
498This parameter is position-free and uses a backtick routine align (@`align@). The parameter passed with @`align@ should be of type @size_t@. If the alignment parameter is not a power of two or is less than the default alignment of the allocator (that can be found out using routine libAlign in \CFA) then the passed alignment parameter will be rejected and the default alignment will be used.
499
500Example: @int b = alloc( 5 , 64`align )@
501This call will return a dynamic array of five integers. It will align the allocated object to 64.
502
503\paragraph{Fill}
504This parameter is position-free and uses a backtick routine fill (@`fill@). In case of @realloc@, only the extra space after copying the data in the old object will be filled with given parameter.
505Three types of parameters can be passed using `fill.
506
507\begin{itemize}
508\item
509@char@: A char can be passed with @`fill@ to fill the whole dynamic allocation with the given char recursively till the end of required allocation.
510\item
511Object of returned type: An object of type of returned type can be passed with @`fill@ to fill the whole dynamic allocation with the given object recursively till the end of required allocation.
512\item
513Dynamic object of returned type: A dynamic object of type of returned type can be passed with @`fill@ to fill the dynamic allocation with the given dynamic object. In this case, the allocated memory is not filled recursively till the end of allocation. The filling happen untill the end object passed to @`fill@ or the end of requested allocation reaches.
514\end{itemize}
515
516Example: @int b = alloc( 5 , 'a'`fill )@
517This call will return a dynamic array of five integers. It will fill the allocated object with character 'a' recursively till the end of requested allocation size.
518
519Example: @int b = alloc( 5 , 4`fill )@
520This call will return a dynamic array of five integers. It will fill the allocated object with integer 4 recursively till the end of requested allocation size.
521
522Example: @int b = alloc( 5 , a`fill )@ where @a@ is a pointer of int type
523This call will return a dynamic array of five integers. It will copy data in a to the returned object non-recursively untill end of a or the newly allocated object is reached.
524
525\paragraph{Resize}
526This parameter is position-free and uses a backtick routine resize (@`resize@). It represents the old dynamic object (oaddr) that the programmer wants to
527\begin{itemize}
528\item
529resize to a new size.
530\item
531realign to a new alignment
532\item
533fill with something.
534\end{itemize}
535The data in old dynamic object will not be preserved in the new object. The type of object passed to @`resize@ and the returned type of alloc call can be different.
536
537Example: @int b = alloc( 5 , a`resize )@
538This call will resize object a to a dynamic array that can contain 5 integers.
539
540Example: @int b = alloc( 5 , a`resize , 32`align )@
541This call will resize object a to a dynamic array that can contain 5 integers. The returned object will also be aligned to 32.
542
543Example: @int b = alloc( 5 , a`resize , 32`align , 2`fill )@
544This call will resize object a to a dynamic array that can contain 5 integers. The returned object will also be aligned to 32 and will be filled with 2.
545
546\paragraph{Realloc}
547This parameter is position-free and uses a backtick routine @realloc@ (@`realloc@). It represents the old dynamic object (oaddr) that the programmer wants to
548\begin{itemize}
549\item
550realloc to a new size.
551\item
552realign to a new alignment
553\item
554fill with something.
555\end{itemize}
556The data in old dynamic object will be preserved in the new object. The type of object passed to @`realloc@ and the returned type of alloc call cannot be different.
557
558Example: @int b = alloc( 5 , a`realloc )@
559This call will realloc object a to a dynamic array that can contain 5 integers.
560
561Example: @int b = alloc( 5 , a`realloc , 32`align )@
562This call will realloc object a to a dynamic array that can contain 5 integers. The returned object will also be aligned to 32.
563
564Example: @int b = alloc( 5 , a`realloc , 32`align , 2`fill )@
565This call will resize object a to a dynamic array that can contain 5 integers. The returned object will also be aligned to 32. The extra space after copying data of a to the returned object will be filled with 2.
Note: See TracBrowser for help on using the repository browser.