asmalloc.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 141 行
C
141 行
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Memory manipulation routines.
*
****************************************************************************/
/*
if TRMEM is defined, trmem functions are used which will help tracking
memory usage.
*/
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "asmalloc.h"
#include "fatal.h"
#ifdef TRMEM
#include "trmem.h"
static _trmem_hdl memHandle;
static int memFile; /* file handle we'll write() to */
static void memLine( int *fh, const char *buf, unsigned size )
{
write( 2, "***", 3 );
write( 2, buf, size );
if( *fh != -1 ) {
write( *fh, buf, size );
}
}
#endif
void MemInit( void )
{
#ifdef TRMEM
memFile = open( "mem.trk", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
memHandle = _trmem_open( malloc, free, realloc, _expand, &memFile, memLine,
_TRMEM_ALLOC_SIZE_0 |
_TRMEM_FREE_NULL |
_TRMEM_OUT_OF_MEMORY |
_TRMEM_CLOSE_CHECK_FREE
);
if( memHandle == NULL ) {
exit( EXIT_FAILURE );
}
#endif
}
void MemFini( void )
{
#ifdef TRMEM
if( memHandle != NULL ) {
_trmem_prt_list( memHandle );
_trmem_close( memHandle );
if( memFile != -1 ) {
close( memFile );
}
memHandle = NULL;
}
#endif
}
void *AsmAlloc( size_t size )
{
void *ptr;
#ifdef TRMEM
ptr = _trmem_alloc( size, _trmem_guess_who(), memHandle );
#else
ptr = malloc( size );
#endif
if( ptr == NULL ) {
Fatal( MSG_OUT_OF_MEMORY );
}
return( ptr );
}
void AsmFree( void *ptr )
{
if( ptr != NULL ) {
#ifdef TRMEM
_trmem_free( ptr, _trmem_guess_who(), memHandle );
#else
free( ptr );
#endif
}
}
void *MemAlloc( size_t size )
{
void *ptr;
#ifdef TRMEM
ptr = _trmem_alloc( size, _trmem_guess_who(), memHandle );
#else
ptr = malloc( size );
#endif
if( ptr == NULL ) {
Fatal( MSG_OUT_OF_MEMORY );
}
return( ptr );
}
void MemFree( void *ptr )
{
if( ptr != NULL ) {
#ifdef TRMEM
_trmem_free( ptr, _trmem_guess_who(), memHandle );
#else
free( ptr );
#endif
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?