Skip to content

Commit

Permalink
Added required sanity checks for managed memory functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
BigETI committed Aug 15, 2018
1 parent f578e8f commit 8d458f9
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 116 deletions.
28 changes: 14 additions & 14 deletions include/memory.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ enum EMemoryResult
EMemoryResult_OutOfMemory
}

// Allocate new memory. Delete with `MEM_delete`
// Allocate new memory. Delete with "MEM_delete"
native Pointer:MEM_new(cells = 1);

// Allocate new zeroed memory. Delete with `MEM_delete`
// Allocate new zeroed memory. Delete with "MEM_delete"
native Pointer:MEM_new_zero(cells = 1);

// Allocate new value. Delete with `MEM_delete`
// Allocate new value. Delete with "MEM_delete"
native Pointer:MEM_new_val(value);

// Allocate new array. Delete with `MEM_delete`
// Allocate new array. Delete with "MEM_delete"
native Pointer:MEM_new_arr(const arr[], arr_size = sizeof arr);

// Clone memory. Delete with `MEM_delete`
// Clone memory. Delete with "MEM_delete"
native Pointer:MEM_clone(Pointer:pointer);

// Delete allocated memory
Expand Down Expand Up @@ -84,34 +84,34 @@ native Pointer:MEM_zero(Pointer:pointer, size, index = 0);
// Get last result
native EMemoryResult:MEM_get_last_result();

// Allocate new unmanaged memory. Delete with `MEM_UM_delete`
// Allocate new unmanaged memory. Delete with "MEM_UM_delete"
native UnmanagedPointer:MEM_UM_new(cells = 1);

// Allocate new zeroed unmanaged memory. Delete with `MEM_UM_delete`
// Allocate new zeroed unmanaged memory. Delete with "MEM_UM_delete"
native UnmanagedPointer:MEM_UM_new_zero(cells = 1);

// Allocate new unmanaged value. Delete with `MEM_UM_delete`
// Allocate new unmanaged value. Delete with "MEM_UM_delete"
native UnmanagedPointer:MEM_UM_new_val(value);

// Allocate new unmanaged array. Delete with `MEM_UM_delete`
// Allocate new unmanaged array. Delete with "MEM_UM_delete"
native UnmanagedPointer:MEM_UM_new_arr(const arr[], arr_size = sizeof arr);

// Clone unmanaged memory. Delete with `MEM_UM_delete`
// Clone unmanaged memory. Delete with "MEM_UM_delete"
native UnmanagedPointer:MEM_UM_clone(AnyPointer:pointer, index = 0, cells);

// Delete unmanaged memory
native MEM_UM_delete(UnmanagedPointer:pointer);

// Get unmanaged value
// Get value from unmanaged memory
native MEM_UM_get_val(AnyPointer:pointer, index = 0);

// Get unmanaged array
// Get array from unmanaged memory
native ForeignPointer:MEM_UM_get_arr(AnyPointer:pointer, index = 0, arr[], arr_size = sizeof arr);

// Set unmanaged value
// Set value at unmanaged memory
native MEM_UM_set_val(UnmanagedPointer:pointer, index = 0, value);

// Set unmanaged array
// Set array at unmanaged memory
native ForeignPointer:MEM_UM_set_arr(UnmanagedPointer:pointer, index = 0, const arr[], arr_size = sizeof arr);

// Copy unmanaged memory
Expand Down
32 changes: 24 additions & 8 deletions pawn-memory/EMemoryResult.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
#ifndef __PAWN_MEMORY_E_MEMORY_RESULT_H__
# define __PAWN_MEMORY_E_MEMORY_RESULT_H__

// PAWN memory namespace
/// <summary>
/// PAWN memory namespace
/// </summary>
namespace PAWNMemory
{
// Memory result enumerator
/// <summary>
/// Memory result enumerator
/// </summary>
enum EMemoryResult
{
// OK
/// <summary>
/// OK
/// </summary>
EMemoryResult_OK,

// Invalid size
/// <summary>
/// Invalid size
/// </summary>
EMemoryResult_InvalidSize,

// Invalid pointer
/// <summary>
/// Invalid pointer
/// </summary>
EMemoryResult_InvalidPointer,

// Invalid index
/// <summary>
/// Invalid index
/// </summary>
EMemoryResult_InvalidIndex,

// Invalid index and size
/// <summary>
/// Invalid index and size
/// </summary>
EMemoryResult_InvalidIndexSize,

// Out of memory
/// <summary>
/// Out of memory
/// </summary>
EMemoryResult_OutOfMemory
};
}
Expand Down
55 changes: 54 additions & 1 deletion pawn-memory/ManagedMemory.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include "ManagedMemory.h"
#include <string.h>

using namespace PAWNMemory;
using namespace std;
using namespace PAWNMemory;

/// <summary>
/// Pointers
/// </summary>
map<cell *, cell> ManagedMemory::pointers;

/// <summary>
/// Last result
/// </summary>
EMemoryResult ManagedMemory::lastResult(EMemoryResult_OK);

/// <summary>
/// New
/// </summary>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
cell * ManagedMemory::New(cell size)
{
cell *ret(nullptr);
Expand Down Expand Up @@ -39,6 +50,11 @@ cell * ManagedMemory::New(cell size)
return ret;
}

/// <summary>
/// New zero
/// </summary>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
cell * ManagedMemory::NewZero(cell size)
{
cell *ret(nullptr);
Expand Down Expand Up @@ -71,6 +87,11 @@ cell * ManagedMemory::NewZero(cell size)
return ret;
}

/// <summary>
/// New value
/// </summary>
/// <param name="val">Value</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
cell * ManagedMemory::NewValue(cell val)
{
cell *ret(nullptr);
Expand All @@ -96,6 +117,12 @@ cell * ManagedMemory::NewValue(cell val)
return ret;
}

/// <summary>
/// New array
/// </summary>
/// <param name="arr">Array</param>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
cell * ManagedMemory::NewArray(cell * arr, cell size)
{
cell *ret(nullptr);
Expand Down Expand Up @@ -128,6 +155,11 @@ cell * ManagedMemory::NewArray(cell * arr, cell size)
return ret;
}

/// <summary>
/// Clone
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
cell * ManagedMemory::Clone(cell * ptr)
{
cell *ret(nullptr);
Expand Down Expand Up @@ -161,6 +193,10 @@ cell * ManagedMemory::Clone(cell * ptr)
return ret;
}

/// <summary>
/// Delete
/// </summary>
/// <param name="ptr">Pointer</param>
void ManagedMemory::Delete(cell * ptr)
{
map<cell *, cell>::iterator it(pointers.find(ptr));
Expand All @@ -176,12 +212,22 @@ void ManagedMemory::Delete(cell * ptr)
}
}

/// <summary>
/// Is valid pointer
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>"true" if pointer is valid, otherwise "false"</returns>
bool ManagedMemory::IsValidPointer(cell * ptr)
{
lastResult = EMemoryResult_OK;
return (pointers.find(ptr) != pointers.end());
}

/// <summary>
/// Get size
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>Size of allocated memory</returns>
cell ManagedMemory::GetSize(cell * ptr)
{
cell ret(0);
Expand All @@ -198,6 +244,9 @@ cell ManagedMemory::GetSize(cell * ptr)
return ret;
}

/// <summary>
/// Clear
/// </summary>
void ManagedMemory::Clear()
{
for (pair<cell *, cell> pointer : pointers)
Expand All @@ -208,6 +257,10 @@ void ManagedMemory::Clear()
lastResult = EMemoryResult_OK;
}

/// <summary>
/// Get last result
/// </summary>
/// <returns>Last result</returns>
EMemoryResult ManagedMemory::GetLastResult()
{
EMemoryResult ret(lastResult);
Expand Down
92 changes: 74 additions & 18 deletions pawn-memory/ManagedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,118 @@
# include "SDK/plugin.h"
# include "EMemoryResult.h"

// PAWN memory namespace
/// <summary>
/// PAWN memory namespace
/// </summary>
namespace PAWNMemory
{
// Managed memory class
/// <summary>
/// Managed memory class
/// </summary>
class ManagedMemory
{
private:

// Pointers
/// <summary>
/// Pointers
/// </summary>
static std::map<cell *, cell> pointers;

// Last result
/// <summary>
/// Last result
/// </summary>
static EMemoryResult lastResult;

// Default constructor
/// <summary>
/// Default constructor
/// </summary>
ManagedMemory();

// Copy constructor
/// <summary>
/// Copy constructor
/// </summary>
/// <param name=""></param>
ManagedMemory(const ManagedMemory &);

// Destructor
/// <summary>
/// Destructor
/// </summary>
~ManagedMemory();

// Assign operator
/// <summary>
/// Assign operator
/// </summary>
/// <param name=""></param>
/// <returns>This object</returns>
ManagedMemory & operator = (const ManagedMemory &);

public:

// New
/// <summary>
/// New
/// </summary>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
static cell *New(cell size);

// New zero
/// <summary>
/// New zero
/// </summary>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
static cell *NewZero(cell size);

// New value
/// <summary>
/// New value
/// </summary>
/// <param name="val">Value</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
static cell *NewValue(cell val);

// New array
/// <summary>
/// New array
/// </summary>
/// <param name="arr">Array</param>
/// <param name="size">Size</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
static cell *NewArray(cell * arr, cell size);

// Clone
/// <summary>
/// Clone
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>Pointer of allocated memory if successful, otherwise "nullptr"</returns>
static cell *Clone(cell * ptr);

// Delete
/// <summary>
/// Delete
/// </summary>
/// <param name="ptr">Pointer</param>
static void Delete(cell * ptr);

// Is valid pointer
/// <summary>
/// Is valid pointer
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>"true" if pointer is valid, otherwise "false"</returns>
static bool IsValidPointer(cell * ptr);

// Get size
/// <summary>
/// Get size
/// </summary>
/// <param name="ptr">Pointer</param>
/// <returns>Size of allocated memory</returns>
static cell GetSize(cell * ptr);

// Clear
/// <summary>
/// Clear
/// </summary>
static void Clear();

// Get last result
/// <summary>
/// Get last result
/// </summary>
/// <returns>Last result</returns>
static EMemoryResult GetLastResult();
};
}
Expand Down
Loading

0 comments on commit 8d458f9

Please sign in to comment.