exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d_mem.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008, 2009 The Regents of the University of California.
3  * All rights reserved."
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose, without fee, and without written agreement is
7  * hereby granted, provided that the above copyright notice, the following
8  * two paragraphs and the author appear in all copies of this software.
9  *
10  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
20  *
21  */
22 
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "d_mem.h"
27 
29 
31 
32 void d_malloc_init() {
33  bndrt_t *b = (bndrt_t *)heap;
35 }
36 
38 {
39  uint16_t tmpUsg = memUsage;
40  memUsage = 0;
41  return tmpUsg;
42 }
43 
44 void* d_malloc(uint16_t sz) {
45  bndrt_t* cur = (bndrt_t*) heap;
46  sz += sizeof(bndrt_t) * 2;
47  sz += (sz % D_MEM_ALIGN);
48 
49  while (((*cur & D_MEM_LEN) < sz || (*cur & D_MEM_INUSE) != 0)
50  && (uint8_t *)cur - heap < D_MEM_HEAP_SIZE) {
51  cur = (bndrt_t *)(((uint8_t *)cur) + ((*cur) & D_MEM_LEN));
52  }
53 
54  if ((uint8_t *)cur < heap + D_MEM_HEAP_SIZE) {
55  bndrt_t oldsize = *cur & D_MEM_LEN;
56  bndrt_t *next;
57  sz -= sizeof(bndrt_t);
58  next = ((bndrt_t *)(((uint8_t *)cur) + sz));
59 
60  *cur = (sz & D_MEM_LEN) | D_MEM_INUSE;
61  *next = (oldsize - sz) & D_MEM_LEN;
62 
64  {
66  }
67  return cur + 1;
68  } else return NULL;
69 }
70 
71 void d_free(void *ptr) {
72  bndrt_t *prev = NULL, *cur, *next = NULL;
73  cur = (bndrt_t *)heap;
74 
75  while (cur + 1 != ptr && (uint8_t *)cur - heap < D_MEM_HEAP_SIZE) {
76  prev = cur;
77  cur = (bndrt_t *)(((uint8_t *)cur) + ((*cur) & D_MEM_LEN));
78  }
79  if (cur + 1 == ptr) {
80  next = (bndrt_t *)((*cur & D_MEM_LEN) + ((uint8_t *)cur));
81 
82  *cur &= ~D_MEM_INUSE;
83  if ((((uint8_t *)next) - heap) < D_MEM_HEAP_SIZE &&
84  (*next & D_MEM_INUSE) == 0) {
85  *cur = (*cur & D_MEM_LEN) + (*next & D_MEM_LEN);
86  }
87  if (prev != NULL && (*prev & D_MEM_INUSE) == 0) {
88  *prev = (*prev & D_MEM_LEN) + (*cur & D_MEM_LEN);
89  }
90  }
91 }
92 
94  uint16_t ret = 0;
95  bndrt_t *cur = (bndrt_t *)heap;
96 
97  while ((uint8_t *)cur - heap < D_MEM_HEAP_SIZE) {
98  if ((*cur & D_MEM_INUSE) == 0)
99  ret += *cur & D_MEM_LEN;
100  cur = (bndrt_t *)(((uint8_t *)cur) + ((*cur) & D_MEM_LEN));
101  }
102  return ret;
103 }
104 
105 void* d_realloc(void *ptr, uint16_t size)
106 {
107  void* p_ptr = d_malloc(size);
108  if(p_ptr == NULL)
109  return NULL;
110 
111  memcpy(p_ptr, ptr, size);
112 
113  d_free(ptr);
114  return p_ptr;
115 }