All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bootmemalloc.h
Go to the documentation of this file.
1 /*
2  * bootmemalloc.h
3  *
4  * Created on: Jul 8, 2015
5  * Author: root
6  */
7 
8 #pragma once
9 
10 #include <cvmx.h>
11 #include <cvmx-bootmem.h>
12 
14 {
15 
16 public:
17 
18  enum EGlobal
19  {
22  };
23 
24  struct TEntry
25  {
28  uint8_t* pucMem;
29  };
30 
32  {
33  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
34  {
35  m_tEntries[iIndex].ulSize = 0;
36  m_tEntries[iIndex].cName[0] = '\0';
37  m_tEntries[iIndex].pucMem = NULL;
38  }
39  }
40 
41  void Add(const char* pcName, uint32_t ulSize)
42  {
43  // Check if name allready in use
44  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
45  {
46  if ((m_tEntries[iIndex].ulSize != 0) && (strcmp(pcName, m_tEntries[iIndex].cName)==0))
47  {
48  printf("CBootMemAlloc::Add() Name in use (%s)", pcName);
49  exit(-1);
50  }
51  }
52 
53  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
54  {
55  if (m_tEntries[iIndex].ulSize == 0)
56  {
57  snprintf(m_tEntries[iIndex].cName, MAX_NAME_LENGTH, "%s", pcName);
58  uint32_t ulBlocks = ulSize / CVMX_CACHE_LINE_SIZE;
59  ulBlocks += (ulSize % CVMX_CACHE_LINE_SIZE) ? 1 : 0;
60  m_tEntries[iIndex].ulSize = ulBlocks * CVMX_CACHE_LINE_SIZE;
61  return;
62  }
63  }
64  printf("CBootMemAlloc::Add() No free memory (%s)", pcName);
65  exit(-1);
66  }
67 
68  void* Alloc(const char* pcMemoryName)
69  {
70  uint8_t* pucMem = NULL;
71  uint32_t uxBytes = 0;
72  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
73  {
74  if (m_tEntries[iIndex].ulSize)
75  {
76  uxBytes+= m_tEntries[iIndex].ulSize;
77  }
78  else
79  {
80  break;
81  }
82  }
83  pucMem = (uint8_t*)cvmx_bootmem_alloc_named(uxBytes, 128, pcMemoryName);
84  if (pucMem == NULL)
85  {
86  if (cvmx_bootmem_find_named_block(pcMemoryName))
87  {
88  pucMem = (uint8_t*)cvmx_phys_to_ptr(cvmx_bootmem_find_named_block(pcMemoryName)->base_addr);
89  }
90  else
91  {
92  printf("CBootMemAlloc::Alloc() Unable to find bootmem named block (%s)", pcMemoryName);
93  exit(-1);
94  }
95  }
96  else
97  {
98  //printf("CBootMemAlloc::Deprecated please use CBootMem for %s\n", pcMemoryName);
99  memset(pucMem, 0, uxBytes); // Init all to zero
100  }
101 
102  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
103  {
104  if (m_tEntries[iIndex].ulSize)
105  {
106  if (iIndex == 0)
107  {
108  m_tEntries[iIndex].pucMem = pucMem;
109  }
110  else
111  {
112  m_tEntries[iIndex].pucMem = m_tEntries[iIndex-1].pucMem + m_tEntries[iIndex-1].ulSize;
113  }
114  }
115  else
116  {
117  break;
118  }
119  }
120  return pucMem;
121  }
122 
123  static int Free(const char* pcMemoryName)
124  {
125  return cvmx_bootmem_free_named(pcMemoryName);
126  }
127 
128  void* GetMemoryPointer(const char* pcName)
129  {
130  for (int iIndex = 0; iIndex < MAX_ENTRIES; iIndex++)
131  {
132  if ((m_tEntries[iIndex].ulSize != 0) && (strcmp(pcName, m_tEntries[iIndex].cName)==0))
133  {
134  return m_tEntries[iIndex].pucMem;
135  }
136  }
137  return NULL;
138  }
139 
140 private:
141 
142  TEntry m_tEntries[MAX_ENTRIES];
143 
144 };
static int Free(const char *pcMemoryName)
Definition: bootmemalloc.h:123
void * Alloc(const char *pcMemoryName)
Definition: bootmemalloc.h:68
char cName[MAX_NAME_LENGTH]
Definition: bootmemalloc.h:26
void * GetMemoryPointer(const char *pcName)
Definition: bootmemalloc.h:128
void Add(const char *pcName, uint32_t ulSize)
Definition: bootmemalloc.h:41