a25aa89e89637c5a1d0feccc2e0deec04b1f0084
[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   ElfuPhdr *mp;
33   ElfuScn *ms;
34
35   assert(me);
36
37   CIRCLEQ_INIT(&me->phdrList);
38   CIRCLEQ_INIT(&me->orphanScnList);
39
40   CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
41     CIRCLEQ_REMOVE(&me->phdrList, mp, elem);
42     elfu_mPhdrDestroy(mp);
43   }
44
45   CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
46     CIRCLEQ_REMOVE(&me->orphanScnList, ms, elemChildScn);
47     elfu_mScnDestroy(ms);
48   }
49
50   free(me);
51 }