Aex.Uni's Core Library
This library consists of an
(open-source) SDK featuring ANSI and Unicode string support, and a garbage collector. Coming soon, a linked list manager, and an array sorter.
Here are the contents of it currently, place these files wherever, preferably:
C:\Aex.Uni\Core\Include.
core_base.h
#ifndef _CORE_BASE_H_
#define _CORE_BASE_H_
// Windows
#if (!defined(CORE_NO_WINDOWS))
#include <windows.h>
#endif
// Standard Library
#if (!defined(CORE_NO_STANDARDLIB))
#include <stdio.h>
#include <stdlib.h>
#endif
// Undefines
#ifdef CORE_VERSION
#undef CORE_VERSION
#endif
#ifdef MAX_STRING
#undef MAX_STRING
#endif
#ifdef SAFE_FREE_PTR
#undef SAFE_FREE_PTR
#endif
#ifdef SAFE_RELEASE_PTR
#undef SAFE_RELEASE_PTR
#endif
// Defines
#define CORE_VERSION 0x02030801 // February 3rd 2008 - Build 1
#define MAX_STRING 256
#define SAFE_FREE_PTR(p) if (p != NULL) { free((void*)p); p = NULL; }
#define SAFE_RELEASE_PTR(p) if (p != NULL) { p->Release(); p = NULL; }
#endif
core_string.h
#ifndef _CORE_STRING_H_
#define _CORE_STRING_H_
// Main include
#include "core_base.h"
// Type-Defines
#if (defined(_UNICODE) || defined(UNICODE))
typedef wchar_t _char;
#else
typedef char _char;
#endif
// String Structure
struct string
{
long id;
long m_Length;
_char* m_str;
inline string();
inline string(_char* str);
inline string(const string& str);
inline ~string();
inline long length(void);
inline _char* c_str();
inline const string& operator=(_char* str);
inline const string& operator=(const string& str);
inline _char* operator+(const _char* str);
inline _char* operator+(const string& str);
};
inline string::string()
{
m_Length = 0;
m_str = NULL;
}
inline string::string(_char* str)
{
long strlength = 0;
while(str[strlength] != 0) { strlength++; }
m_Length = strlength;
if (m_Length > 0)
{
m_str = (_char*)malloc(sizeof(_char) * (strlength+1));
for(long i = 0; i < strlength; i++)
{
m_str[i] = str[i];
}
m_str[i] = 0;
}
else
{
m_str = "\0";
}
}
inline string::string(const string& str)
{
long strlength = 0;
while(str.m_str[strlength] != 0) { strlength++; }
m_Length = strlength;
if (m_Length > 0)
{
m_str = (_char*)malloc(sizeof(_char) * (strlength+1));
for(long i = 0; i < strlength; i++)
{
m_str[i] = str.m_str[i];
}
m_str[i] = 0;
}
else
{
m_str = "\0";
}
}
inline string::~string()
{
if (m_str != NULL)
{
free((void*)m_str);
m_str = NULL;
}
m_Length = 0;
}
inline long string::length(void)
{
return m_Length;
}
inline _char* string::c_str(void)
{
return m_str;
}
inline const string& string::operator=(_char* str)
{
if (m_str != NULL)
{
free((void*)m_str);
}
if (str != NULL)
{
// Data
long strlength = 0;
long size = 0;
long i = 0;
// Retrieve the string's length
while(str[strlength] != 0) { strlength++; }
m_Length = strlength;
if (m_Length > 0)
{
// Get how big the string is (in bytes + 1 null character byte's size)
size = sizeof(_char) * (strlength + 1);
// Allocate the memory for the string
m_str = (_char*)malloc(size);
// Copy over the data of the other string into this string, up until the NULL byte, then set the null byte.
while(str[i] != 0) { m_str[i] = str[i]; i++; } m_str[i] = 0;
}
else
{
m_str = "\0";
}
}
else
{
m_str = "\0";
}
return *this;
}
inline const string& string::operator=(const string& str)
{
if (m_str != NULL)
{
free((void*)m_str);
}
_char* _str = str.m_str;
if (_str != NULL)
{
// Data
long strlength = 0;
long size = 0;
long i = 0;
// Retrieve the _string's length
while(_str[strlength] != 0) { strlength++; }
m_Length = strlength;
if (m_Length > 0)
{
// Get how big the _string is (in bytes + 1 null character byte's size)
size = sizeof(_char) * (strlength + 1);
// Allocate the memory for the _string
m_str = (_char*)malloc(size);
// Copy over the data of the other _string into this _string, up until the NULL byte, then set the null byte.
while(_str[i] != 0) { m_str[i] = _str[i]; i++; } m_str[i] = 0;
}
else
{
m_str = "\0";
}
}
else
{
m_str = "\0";
}
return *this;
}
inline _char* string::operator+(const _char* str)
{
if (str != NULL)
{
// Data
long strlength = 0, cstrlength = 0;
long size = 0, size0 = 0, size1 = 0;
long i = 0;
// Retrieve str's length
while(str[strlength] != 0) { strlength++; }
// Get the combined length
cstrlength = strlength + m_Length;
// Get how big this string is (Not counting the null byte)
size0 = sizeof(_char) * m_Length;
// Get how big the other string is (Not counting the null byte)
size1 = sizeof(_char) * strlength;
// Get how big the combined size is (Plus null character)
size = size0 + size1 + sizeof(_char);
// Create a buffer big enough for the entire string
_char* tmp = (_char*)malloc(size);
// Copy the contents of m_str to tmp
for(i = 0; i < m_Length; i++) { tmp[i] = m_str[i]; }
// Copy the contents of str to tmp
for(i = m_Length; i < cstrlength; i++) { tmp[i] = str[i]; }
// Set the last piece to null
tmp[cstrlength] = 0;
// Free the string
if (m_str != NULL)
{
free((void*)m_str);
}
// Set m_Length to clength, and m_str to tmp
m_Length = cstrlength;
m_str = tmp;
}
return m_str;
}
inline _char* string::operator+(const string& str)
{
_char* _str = str.m_str;
if (_str != NULL)
{
// Data
long strlength = 0, cstrlength = 0;
long size = 0, size0 = 0, size1 = 0;
long i = 0;
// Retrieve _str's length
while(_str[strlength] != 0) { strlength++; }
// Get the combined length
cstrlength = strlength + m_Length;
// Get how big this _string is (Not counting the null byte)
size0 = sizeof(_char) * m_Length;
// Get how big the other _string is (Not counting the null byte)
size1 = sizeof(_char) * strlength;
// Get how big the combined size is (Plus null character)
size = size0 + size1 + sizeof(_char);
// Create a buffer big enough for the entire _string
_char* tmp = (_char*)malloc(size);
// Copy the contents of m_str to tmp
for(i = 0; i < m_Length; i++) { tmp[i] = m_str[i]; }
// Copy the contents of _str to tmp
long n = 0;
for(i = m_Length; i < cstrlength; i++) { tmp[i] = _str[n]; n++; }
// Set the last piece to null
tmp[cstrlength] = 0;
// Free the _string
if (m_str != NULL)
{
free((void*)m_str);
}
// Set m_Length to cstrlength, and m_str to tmp
m_Length = cstrlength;
m_str = tmp;
}
return m_str;
}
#endif
core_gc.h
#ifndef _CORE_GARBAGECOLLECTOR_H_
#define _CORE_GARBAGECOLLECTOR_H_
// Main include
#include "core_base.h"
// -- Garbage Collector ~ Start ~ --
struct garbageCollector
{
void** Garbage;
unsigned long Size;
unsigned long Current;
inline garbageCollector();
inline ~garbageCollector();
inline void Release(void);
inline void Add(void* pp);
inline void Remove(void* pp);
};
// Release all the memory in the garbage collector
inline void garbageCollector::Release(void)
{
unsigned long i;
for(i=0; i<Size; i++)
{
if (Garbage[i] != NULL)
{
HeapFree(GetProcessHeap(), 0, Garbage[i]);
Garbage[i] = NULL;
}
}
}
// Garbage collector constructor
inline garbageCollector::garbageCollector()
{
Garbage = (void**)HeapAlloc(GetProcessHeap(), 0, 4);
Current = 0;
Size = 0;
}
// Garbage collector destructor
inline garbageCollector::~garbageCollector()
{
unsigned long i;
for(i=0; i<Size; i++)
{
if (Garbage[i] != NULL)
{
HeapFree(GetProcessHeap(), 0, Garbage[i]);
Garbage[i] = NULL;
}
}
}
// Add a pointer to the garbage collector
inline void garbageCollector::Add(void* pp)
{
Garbage = (void**)HeapReAlloc(GetProcessHeap(), 0, (void*)Garbage, (Size + 1) * 4);
Garbage[Current] = pp;
Current++;
Size++;
}
// Remove a pointer from the garbage collector
inline void garbageCollector::Remove(void* pp)
{
int i = 0;
int fReady = 0;
int fResult = 0;
while (fReady == 0)
{
if (Garbage[i] == pp)
{
HeapFree(GetProcessHeap(), 0, pp);
Garbage[i] = NULL;
fReady = 1;
fResult = 1;
}
if (i < (int)Size)
{
i++;
}
else
{
fReady = 1;
}
}
}
inline void* AllocMemGC(garbageCollector* gc, unsigned long int numBytes)
{
void* tmp = HeapAlloc(GetProcessHeap(),0,numBytes);
if (tmp != NULL)
{
memset(tmp, 0, numBytes);
gc->Add(tmp);
return tmp;
}
return NULL;
}
inline void FreeMemGC(garbageCollector* gc, void* pPtr)
{
if (pPtr != NULL)
{
gc->Remove(pPtr);
}
}
// -- Garbage Collector ~ End ~ --
// -- Garbage Collector (COM) ~ Start ~ --
struct garbageCollectorCOM
{
void** Garbage;
unsigned long Size;
unsigned long Current;
inline garbageCollectorCOM();
inline ~garbageCollectorCOM();
inline void Release(void);
inline void Add(void* pp);
inline void Remove(void* pp);
};
// COM garbage collector constructor
inline garbageCollectorCOM::garbageCollectorCOM()
{
Garbage = (void**)HeapAlloc(GetProcessHeap(), 0, 4);
Current = 0;
Size = 0;
}
// COM garbage collector destructor
inline garbageCollectorCOM::~garbageCollectorCOM()
{
unsigned long i;
for(i=0; i<Size; i++)
{
if (Garbage[i] != NULL)
{
((IUnknown*)Garbage[i])->Release();
Garbage[i] = NULL;
}
}
}
// Remove all memory inside the garbage collector
inline void garbageCollectorCOM::Release(void)
{
unsigned long i;
for(i=0; i<Size; i++)
{
if (Garbage[i] != NULL)
{
((IUnknown*)Garbage[i])->Release();
Garbage[i] = NULL;
}
}
}
// Add a COM pointer to the garbage collector
inline void garbageCollectorCOM::Add(void* pp)
{
Garbage = (void**)HeapReAlloc(GetProcessHeap(), 0, (void*)Garbage, (Size + 1) * 4);
Garbage[Current] = pp;
Current++;
Size++;
}
// Remove and release a COM pointer in the garbage collector
inline void garbageCollectorCOM::Remove(void* pp)
{
int i = 0;
int fReady = 0;
int fResult = 0;
while (fReady == 0)
{
if (Garbage[i] == pp)
{
((IUnknown*)Garbage[i])->Release();
Garbage[i] = NULL;
fReady = 1;
fResult = 1;
}
if (i < (int)Size)
{
i++;
}
else
{
fReady = 1;
}
}
}
#endif
Hopefully the code should explain it self, any questions feel free to ask.
Cheers,
-naota
With any luck you'll be able to turn a fully functioning program to a crashing program with just a little bit of coding.
Aex.Uni forums