Implement memory management TODOs
[centaur.git] / src / libelfu / model / elf.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <libelfu/libelfu.h>
5
6
7 /*
8  * Allocation, destruction
9  */
10
11 ElfuElf* elfu_mElfAlloc()
12 {
13   ElfuElf *me;
14
15   me = malloc(sizeof(ElfuElf));
16   if (!me) {
17     ELFU_WARN("mElfAlloc: malloc() failed for ElfuElf.\n");
18     return NULL;
19   }
20
21   memset(me, 0, sizeof(*me));
22
23   CIRCLEQ_INIT(&me->phdrList);
24   CIRCLEQ_INIT(&me->orphanScnList);
25
26   return me;
27 }
28
29
30 void elfu_mElfDestroy(ElfuElf* me)
31 {
32   assert(me);
33
34   if (!CIRCLEQ_EMPTY(&me->phdrList)) {
35     ElfuPhdr *nextmp;
36
37     nextmp = CIRCLEQ_FIRST(&me->phdrList);
38     while ((void*)nextmp != (void*)&me->phdrList) {
39       ElfuPhdr *curmp = nextmp;
40       nextmp = CIRCLEQ_NEXT(curmp, elem);
41       CIRCLEQ_REMOVE(&me->phdrList, curmp, elem);
42       elfu_mPhdrDestroy(curmp);
43     }
44   }
45
46   if (!CIRCLEQ_EMPTY(&me->orphanScnList)) {
47     ElfuScn *nextms;
48
49     nextms = CIRCLEQ_FIRST(&me->orphanScnList);
50     while ((void*)nextms != (void*)&me->orphanScnList) {
51       ElfuScn *curms = nextms;
52       nextms = CIRCLEQ_NEXT(curms, elemChildScn);
53       CIRCLEQ_REMOVE(&me->orphanScnList, curms, elemChildScn);
54       elfu_mScnDestroy(curms);
55     }
56   }
57
58   free(me);
59 }