Mark3 Realtime Kernel
autoalloc.cpp
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 =========================================================================== */
19 #include "mark3.h"
20 
21 #include <stdint.h>
22 
23 //---------------------------------------------------------------------------
24 // Override new() and delete() using functions provided to AutoAlloc
25 //---------------------------------------------------------------------------
26 #if PORT_OVERLOAD_NEW
27 using namespace Mark3;
28 void* operator new(size_t n)
29 {
30  return AutoAlloc::NewRawData(n);
31 }
32 
33 //---------------------------------------------------------------------------
34 void* operator new[](size_t n)
35 {
36  return AutoAlloc::NewRawData(n);
37 }
38 
39 //---------------------------------------------------------------------------
40 void operator delete(void* p)
41 {
43 }
44 
45 //---------------------------------------------------------------------------
46 void operator delete[](void* p)
47 {
49 }
50 #endif
51 
52 namespace Mark3
53 {
56 
57 //---------------------------------------------------------------------------
58 void* AutoAlloc::Allocate(AutoAllocType eType_, size_t sSize_)
59 {
60  if (!m_pfAllocator) {
61  return nullptr;
62  }
63  return m_pfAllocator(eType_, sSize_);
64 }
65 
66 //---------------------------------------------------------------------------
67 void AutoAlloc::Free(AutoAllocType eType_, void* pvObj_)
68 {
69  if (!m_pfFree) {
70  return;
71  }
72  m_pfFree(eType_, pvObj_);
73 }
74 
75 //---------------------------------------------------------------------------
77 {
78  m_pfAllocator = pfAllocator_;
79  m_pfFree = pfFree_;
80 }
81 
82 //---------------------------------------------------------------------------
84 {
85  m_pfAllocator = nullptr;
86  m_pfFree = nullptr;
87 }
88 
89 //---------------------------------------------------------------------------
90 void* AutoAlloc::NewUserTypeAllocation(uint8_t eType_)
91 {
92  return Allocate(static_cast<AutoAllocType>(eType_), 0);
93 }
94 //---------------------------------------------------------------------------
95 void AutoAlloc::DestroyUserTypeAllocation(uint8_t eUserType_, void* pvObj_)
96 {
97  Free(static_cast<AutoAllocType>(eUserType_), pvObj_);
98 }
99 //---------------------------------------------------------------------------
100 void* AutoAlloc::NewRawData(size_t sSize_)
101 {
102  return Allocate(AutoAllocType::Raw, sSize_);
103 }
104 //---------------------------------------------------------------------------
105 void AutoAlloc::DestroyRawData(void* pvData_)
106 {
107  Free(AutoAllocType::Raw, pvData_);
108 }
109 
110 } // namespace Mark3
void(*)(AutoAllocType eType_, void *pvObj_) AutoAllocFree_t
Definition: autoalloc.h:55
void *(*)(AutoAllocType eType_, size_t sSize_) AutoAllocAllocator_t
Definition: autoalloc.h:54
static void DestroyRawData(void *pvData_)
DestroyRawData Free a previously allocated blob of data allocated via NewRawData() ...
Definition: autoalloc.cpp:105
static void Init(void)
Init Initialize the AutoAllocator before use. Called by Kernel::Init().
Definition: autoalloc.cpp:83
Definition: atomic.cpp:23
AutoAllocType
Definition: autoalloc.h:32
static void DestroyUserTypeAllocation(uint8_t eUserType_, void *pvObj_)
DestroyUserTypeAllocation Free a previously allocated user-defined object.
Definition: autoalloc.cpp:95
static void * NewUserTypeAllocation(uint8_t eUserType_)
NewUserTypeAllocation Attempt to allocate a user-defined object type from the heap.
Definition: autoalloc.cpp:90
Single include file given to users of the Mark3 Kernel API.
static AutoAllocFree_t m_pfFree
Funciton used to free objectss.
Definition: autoalloc.h:148
static AutoAllocAllocator_t m_pfAllocator
Function used to allocate objects.
Definition: autoalloc.h:147
static void Free(AutoAllocType eType_, void *pvObj_)
Definition: autoalloc.cpp:67
static void * NewRawData(size_t sSize_)
NewRawData Attempt to allocate a blob of raw data from the heap.
Definition: autoalloc.cpp:100
static void * Allocate(AutoAllocType eType_, size_t sSize_)
Definition: autoalloc.cpp:58
static void SetAllocatorFunctions(AutoAllocAllocator_t pfAllocator_, AutoAllocFree_t pfFree_)
SetAllocatorFunctions Set the functions used by this class to allocate/free memory used in the kernel...
Definition: autoalloc.cpp:76