www.pudn.com > PtOpenGuiSourceCode.zip > MEM.CPP
//--------------------------------------------------------------------------- // Copyright (c) Allan Petersen, 1999. /* Version 1.0 of the memory handling module mem.dll */ /* * (c) Copyright 1999, Allan Petersen * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Allan Petersen not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL ALLAN * PETERSEN BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT ALLAN PETERSEN HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * */ /* * mem.cpp * includes function : * mem_setup * mem_realloc * mem_malloc * mem_free * * Modification : * -, , , * 01/30/1999, 1.0, Creation of module, AP * * * */ //--------------------------------------------------------------------------- #include #pragma hdrstop #include "mem.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*) { return 1; } //--------------------------------------------------------------------------- static long mem_type = 0; // global alloc //--------------------------------------------------------------------------- // mem_setup () - Setting up type of memory handler used // input // type = 0 : use global memory object. // type = 1 : use borland shared memory object. //--------------------------------------------------------------------------- void _export mem_setup (long type) { mem_type = type; } //--------------------------------------------------------------------------- // mem_realloc () - re allocate existing memory by size, or // create new if NULL pointer received // input // pointer to memory object, and size //--------------------------------------------------------------------------- void * _export mem_realloc (void *ptr, long size) { void *mptr; //... use malloc method if receiving null pointer if (ptr == NULL) return (mem_malloc (size)); //... select memory handler if (mem_type == 0) { //... reallocate memory handler, reassign as moveable and //... init new memory with zero. mptr = GlobalReAlloc (ptr, size, GMEM_ZEROINIT | GMEM_MOVEABLE); } if (mem_type == 1) { //... reallocate memory handler, //... new memory uninitialised mptr = SysReallocMem (ptr, size); } //...Allocation failed, give new memory block if (mptr == NULL) { return (mem_malloc (size)); } //...return memory object return (mptr); } //--------------------------------------------------------------------------- // //--------------------------------------------------------------------------- void * _export mem_malloc (long size) { void *mptr; char str[256]; if (mem_type == 0) { mptr = GlobalAlloc (GMEM_ZEROINIT| GMEM_FIXED, size); } if (mem_type == 1) { mptr = SysGetMem (size); } if (mptr == NULL) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, str, 255, NULL); ShowMessage (str); } return (mptr); } //--------------------------------------------------------------------------- void _export mem_free (void *ptr) { char str[256]; if (ptr != NULL) { if (mem_type == 0) { if (GlobalFree (ptr) != NULL) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, str, 255, NULL); ShowMessage (str); } } if (mem_type == 1) { if (SysFreeMem (ptr) != NULL) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, str, 255, NULL); ShowMessage (str); } } } }