ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dce-alloc.cc
Go to the documentation of this file.
1 #include "dce-stdlib.h"
2 #include "dce-unistd.h"
3 #include "utils.h"
4 #include "process.h"
5 #include "kingsley-alloc.h"
6 #include "ns3/log.h"
7 #include <string.h>
8 
9 NS_LOG_COMPONENT_DEFINE ("SimuAlloc");
10 
11 using namespace ns3;
12 
13 void * dce_calloc (size_t nmemb, size_t size)
14 {
15  GET_CURRENT (nmemb << size);
16  void *ptr = dce_malloc (nmemb * size);
17  memset (ptr, 0, nmemb * size);
18  return ptr;
19 }
20 void * dce_malloc (size_t size)
21 {
22  GET_CURRENT (size);
23  size += sizeof (size_t);
24  uint8_t *buffer = current->process->alloc->Malloc (size);
25  memcpy (buffer, &size, sizeof (size_t));
26  buffer += sizeof (size_t);
27  NS_LOG_DEBUG ("alloc=" << (void*)buffer);
28  return buffer;
29 }
30 void dce_free (void *ptr)
31 {
32  GET_CURRENT (ptr);
33  if (ptr == 0)
34  {
35  return;
36  }
37  uint8_t *buffer = (uint8_t*)ptr;
38  size_t size;
39  buffer -= sizeof (size_t);
40  memcpy (&size, buffer, sizeof (size_t));
41  current->process->alloc->Free (buffer, size);
42 }
43 void * dce_realloc (void *ptr, size_t size)
44 {
45  GET_CURRENT (ptr << size);
46  if (ptr == 0 && size == 0)
47  {
48  return 0;
49  }
50  if (ptr == 0)
51  {
52  return dce_malloc (size);
53  }
54  size_t oldSize;
55  uint8_t *buffer = (uint8_t*)ptr;
56  buffer -= sizeof (size_t);
57  size += sizeof (size_t);
58  memcpy (&oldSize, buffer, sizeof (size_t));
59  if (size <= oldSize)
60  {
61  return ptr;
62  }
63  buffer = current->process->alloc->Realloc (buffer, oldSize, size);
64  memcpy (buffer, &size, sizeof (size_t));
65  buffer += sizeof (size_t);
66  return buffer;
67 }
68 void * dce_sbrk (intptr_t increment)
69 {
70  if (0 == increment)
71  {
72  return (void*)-1;
73  }
74  return dce_calloc (1, increment);
75 }
76 int dce_getpagesize (void)
77 {
78  return sysconf (_SC_PAGESIZE);
79 }