aboutsummaryrefslogtreecommitdiff
path: root/lib/NativeHeapApi/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-03-27 02:20:06 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-03-27 02:20:06 -0400
commit6f7f4a4f03c7e62db64c01b2a0b128586bf11dad (patch)
tree2ef00d7d8527f5153ccd4188665bd9b47573cf27 /lib/NativeHeapApi/src
parent6b5ca9e49e33eb3e03d6f7333661da7e6d0546fa (diff)
Native heap api and alloc optimizations
Diffstat (limited to 'lib/NativeHeapApi/src')
-rw-r--r--lib/NativeHeapApi/src/NativeHeapApi.h124
1 files changed, 124 insertions, 0 deletions
diff --git a/lib/NativeHeapApi/src/NativeHeapApi.h b/lib/NativeHeapApi/src/NativeHeapApi.h
new file mode 100644
index 0000000..5e83108
--- /dev/null
+++ b/lib/NativeHeapApi/src/NativeHeapApi.h
@@ -0,0 +1,124 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: NativeHeapApi
+* File: NativeHeapApi.h
+*
+* NativeHeapApi is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* NativeHeapApi is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with NativeHeapApi. If not, see http://www.gnu.org/licenses/.
+*/
+
+#pragma once
+
+#ifndef NATIVE_HEAP_API
+#define NATIVE_HEAP_API
+
+/*
+* Method calling convention for export
+*/
+#ifndef HEAP_METHOD_EXPORT
+ #ifdef WIN32
+ #define HEAP_METHOD_EXPORT __declspec(dllexport)
+ #else
+ #define METHOD_EXPORT
+ #endif
+#endif // HEAP_METHOD_EXPORT!
+
+/// <summary>
+/// Internal heap creation flags passed to the creation method by the library loader
+/// </summary>
+typedef enum HeapCreationFlags
+{
+ /// <summary>
+ /// Default/no flags
+ /// </summary>
+ HEAP_CREATION_NO_FLAGS,
+ /// <summary>
+ /// Specifies that all allocations be zeroed before returning to caller
+ /// </summary>
+ HEAP_CREATION_GLOBAL_ZERO = 0x01,
+ /// <summary>
+ /// Specifies that the heap should use internal locking, aka its not thread safe
+ /// and needs to be made thread safe
+ /// </summary>
+ HEAP_CREATION_SERIALZE_ENABLED = 0x02,
+ /// <summary>
+ /// Specifies that the requested heap will be a shared heap for the process/library
+ /// </summary>
+ HEAP_CREATION_IS_SHARED = 0x04
+} HeapCreationFlags;
+
+/// <summary>
+/// The vnlib ERRNO type, integer/process dependent,
+/// internally represented as a pointer
+/// </summary>
+typedef void* ERRNO;
+
+/// <summary>
+/// A structure for heap initialization
+/// </summary>
+typedef struct UnmanagedHeapFlags
+{
+ LPVOID HeapPointer;
+ HeapCreationFlags CreationFlags;
+ ERRNO Flags;
+} UnmanagedHeapFlags;
+
+/// <summary>
+/// The heap creation method. You must set the flags->HeapPointer = your heap
+/// structure
+/// </summary>
+/// <param name="flags">Creation flags passed by the caller to create the heap. This structure will be initialized, and may be modified</param>
+/// <returns>A boolean value that indicates the result of the operation</returns>
+HEAP_METHOD_EXPORT ERRNO heapCreate(UnmanagedHeapFlags* flags);
+
+/// <summary>
+/// Destroys a previously created heap
+/// </summary>
+/// <param name="heap">The pointer to your custom heap structure from heap creation</param>
+HEAP_METHOD_EXPORT ERRNO heapDestroy(LPVOID heap);
+
+/// <summary>
+/// Allocates a block from the desired heap and returns a pointer
+/// to the block. Optionally zeros the block before returning
+/// </summary>
+/// <param name="heap">A pointer to your heap structure</param>
+/// <param name="elements">The number of elements to allocate</param>
+/// <param name="alignment">The alignment (or size) of each element in bytes</param>
+/// <param name="zero">A flag to zero the block before returning the block</param>
+/// <returns>A pointer to the allocated block</returns>
+HEAP_METHOD_EXPORT LPVOID heapAlloc(LPVOID heap, size_t elements, size_t alignment, BOOL zero);
+
+/// <summary>
+/// Reallocates a block on the desired heap and returns a pointer to the new block. If reallocation
+/// is not supported, you should only return 0 and leave the block unmodified. The data in the valid
+/// size of the block MUST remain unmodified.
+/// </summary>
+/// <param name="heap">A pointer to your heap structure</param>
+/// <param name="block">A pointer to the block to reallocate</param>
+/// <param name="elements">The new size of the block, in elements</param>
+/// <param name="alignment">The element size or block alignment</param>
+/// <param name="zero">A flag to zero the block (or the new size) before returning.</param>
+/// <returns>A pointer to the reallocated block, or zero if the operation failed or is not supported</returns>
+HEAP_METHOD_EXPORT LPVOID heapRealloc(LPVOID heap, LPVOID block, size_t elements, size_t alignment, BOOL zero);
+
+/// <summary>
+/// Frees a previously allocated block on the desired heap.
+/// </summary>
+/// <param name="heap">A pointer to your heap structure</param>
+/// <param name="block">A pointer to the block to free</param>
+/// <returns>A value that indicates the result of the operation, nonzero if success, 0 if a failure occurred </returns>
+HEAP_METHOD_EXPORT ERRNO heapFree(LPVOID heap, LPVOID block);
+
+#endif // !NATIVE_HEAP_API \ No newline at end of file