Mark3 Realtime Kernel
autoalloc.h
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 =========================================================================== */
20 #pragma once
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include "mark3cfg.h"
26 
27 namespace Mark3
28 {
29 //---------------------------------------------------------------------------
30 // Define function pointer types used for interfacing with an external heap.
31 //---------------------------------------------------------------------------
32 enum class AutoAllocType : uint8_t {
33  //-- Kernel object types
34  EventFlag,
35  MailBox,
36  Message,
39  Mutex,
40  Notify,
41  Semaphore,
42  Thread,
43  Timer,
46  Coroutine,
47  //-- Allow for users to define their own object types beginning with AutoAllocType_t::User
48  User,
49  //--
50  Raw = 0xFF
51 };
52 
53 //---------------------------------------------------------------------------
54 using AutoAllocAllocator_t = void* (*)(AutoAllocType eType_, size_t sSize_);
55 using AutoAllocFree_t = void (*)(AutoAllocType eType_, void* pvObj_);
56 
57 //---------------------------------------------------------------------------
58 // Forward declaration of kernel objects that can be auotomatically allocated.
59 
60 
72 class AutoAlloc
73 {
74 public:
80  static void Init(void);
81 
88  static void SetAllocatorFunctions(AutoAllocAllocator_t pfAllocator_, AutoAllocFree_t pfFree_);
89 
93  template <typename T, AutoAllocType e> static T* NewObject()
94  {
95  auto* pvObj = Allocate(e, sizeof(T));
96  if (pvObj) {
97  return new (pvObj) T();
98  }
99  return 0;
100  }
101 
106  template <typename T, AutoAllocType e> static void DestroyObject(T* pObj_)
107  {
108  pObj_->~T();
109  Free(e, pObj_);
110  }
111 
118  static void* NewUserTypeAllocation(uint8_t eUserType_);
119 
126  static void DestroyUserTypeAllocation(uint8_t eUserType_, void* pvObj_);
127 
134  static void* NewRawData(size_t sSize_);
135 
141  static void DestroyRawData(void* pvData_);
142 
143 private:
144  static void* Allocate(AutoAllocType eType_, size_t sSize_);
145  static void Free(AutoAllocType eType_, void* pvObj_);
146 
149 };
150 } // namespace Mark3
void(*)(AutoAllocType eType_, void *pvObj_) AutoAllocFree_t
Definition: autoalloc.h:55
static void DestroyObject(T *pObj_)
Definition: autoalloc.h:106
void *(*)(AutoAllocType eType_, size_t sSize_) AutoAllocAllocator_t
Definition: autoalloc.h:54
static T * NewObject()
Definition: autoalloc.h:93
Definition: atomic.cpp:23
Mark3 Kernel Configuration This file is used to configure the kernel for your specific application in...
AutoAllocType
Definition: autoalloc.h:32
The AutoAlloc class. This class provides an object-allocation interface for both kernel objects and u...
Definition: autoalloc.h:72
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