1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code 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.
OpenMoHAA source code 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 OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// memory.c: Memory manager
#include "q_shared.h"
#include "qcommon.h"
#ifndef DEDICATED
# include "../client/client.h"
#endif
#define ZONEID 0x7331
#define ZONEID_CONST 0xC057
void Z_CheckHeap(void);
typedef struct zonedebug_s {
const char *label;
const char *file;
int line;
int allocSize;
} zonedebug_t;
typedef struct memblock_s {
size_t size; // including the header and possibly tiny fragments
struct memblock_s *next, *prev;
int id; // should be ZONEID
#ifdef ZONE_DEBUG
zonedebug_t d;
#endif
} memblock_t;
typedef struct memconstant_s {
memblock_t b;
unsigned char mem[ 2 ];
} memconstant_t;
memconstant_t emptystring = { sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST, 0, 0 };
memconstant_t numberstring[] =
{
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'0', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'1', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'2', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'3', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'4', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'5', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'6', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'7', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'8', 0} },
{ {sizeof( memconstant_t ), NULL, NULL, ZONEID_CONST}, {'9', 0} },
};
static memblock_t mem_blocks[ TAG_NUM_TOTAL_TAGS ];
/*
========================
Z_EmptyStringPointer
========================
*/
const char *Z_EmptyStringPointer( void )
{
return ( const char * )emptystring.mem;
}
/*
========================
Z_NumberStringPointer
========================
*/
const char *Z_NumberStringPointer( int iNum )
{
return ( const char * )numberstring[ iNum - '0' ].mem;
}
/*
========================
Z_Free
========================
*/
void Z_Free( void *ptr )
{
memblock_t *block = ( memblock_t * )( ( byte * )ptr - sizeof( memblock_t ) );
// don't free constant memory
if( block->id == ZONEID_CONST ) {
return;
}
if( block->id != ZONEID ) {
Com_Error( ERR_FATAL, "Z_Free: freed a pointer without ZONEID" );
}
// check the memory trash tester
#ifndef _DEBUG
if( *( int * )( ( byte * )block + block->size - sizeof( int ) ) != ZONEID ) {
Com_Error( ERR_FATAL, "Z_Free: memory block wrote past end" );
}
#endif
block->next->prev = block->prev;
block->prev->next = block->next;
block->prev = block;
block->next = block;
// free the block
free( block );
}
/*
========================
Z_FreeTags
========================
*/
void Z_FreeTags( int tag )
{
memblock_t *block;
memblock_t *next;
for( block = mem_blocks[ tag ].next; block != &mem_blocks[ tag ]; block = next )
{
next = block->next;
Z_Free( ( ( byte * )block + sizeof( memblock_t ) ) );
}
mem_blocks[ tag ].prev = &mem_blocks[ tag ];
mem_blocks[ tag ].next = &mem_blocks[ tag ];
}
/*
========================
Z_TagMalloc
========================
*/
#ifdef ZONE_DEBUG
void *Z_TagMallocDebug( int size, int tag, const char *label, const char *file, int line ) {
int allocSize;
#else
void *Z_TagMalloc( int size, int tag ) {
#endif
memblock_t *block;
if( size <= 0 )
{
//Z_Meminfo_f();
Com_DPrintf( "Z_TagMalloc, Negative or zero size %d tag %i\n", size, tag );
return NULL;
}
if( tag == TAG_FREE ) {
Com_Error( ERR_FATAL, "Z_TagMalloc: tried to use a 0 tag" );
}
#ifdef ZONE_DEBUG
allocSize = size;
#endif
size += sizeof( memblock_t ); // account for size of block header
#ifndef _DEBUG
size += sizeof( int ); // space for memory trash tester
#endif
size = PAD( size, sizeof( intptr_t ) ); // align to 32/64 bit boundary
block = ( memblock_t * )malloc( size );
block->id = ZONEID;
block->size = size;
block->next = &mem_blocks[ tag ];
block->prev = mem_blocks[ tag ].prev;
block->prev->next = block;
mem_blocks[ tag ].prev = block;
#ifdef ZONE_DEBUG
block->d.label = label;
block->d.file = file;
block->d.line = line;
block->d.allocSize = allocSize;
#endif
#ifndef _DEBUG
// marker for memory trash testing
*( int * )( ( byte * )block + block->size - sizeof( int ) ) = ZONEID;
#endif
return ( void * )( ( byte * )block + sizeof( memblock_t ) );
}
/*
========================
Z_CheckHeap
========================
*/
void Z_CheckHeap( void )
{
#ifndef _DEBUG
int k;
memblock_t *block;
for( k = 0; k < TAG_NUM_TOTAL_TAGS; k++ )
{
for( block = mem_blocks[ k ].next; block != &mem_blocks[ k ]; block = block->next )
{
if( *( int * )( ( byte * )block + block->size - 4 ) != ZONEID ) {
Com_Error( ERR_FATAL, "Z_CheckHeap: memory block wrote past end" );
}
}
}
#endif
}
/*
========================
Z_TouchMemory
Touch all known used data to make sure it is paged in
========================
*/
void Z_TouchMemory( void )
{
size_t i, j;
int k;
int sum;
int start, end;
memblock_t *block;
Z_CheckHeap();
start = Sys_Milliseconds();
sum = 0;
for( k = 0; k < TAG_NUM_TOTAL_TAGS; k++ )
{
for( block = mem_blocks[ k ].next; block != &mem_blocks[ k ]; block = block->next )
{
j = block->size >> 2;
for( i = 0; i < j; i += 64 ) { // only need to touch each page
sum += ( ( int * )block )[ i ];
}
}
}
end = Sys_Milliseconds();
Com_Printf( "Z_TouchMemory: %i msec\n", end - start );
}
/*
========================
Z_Meminfo_f
========================
*/
void Z_Meminfo_f( void )
{
int k;
size_t totalBlocks;
size_t totalBytes;
size_t numBlocks;
size_t numBytes;
memblock_t *block;
totalBlocks = 0;
totalBytes = 0;
Com_Printf( "-------------------------------------------\n" );
for( k = TAG_CONST + 1; k < TAG_NUM_TOTAL_TAGS; k++ )
{
numBlocks = 0;
numBytes = 0;
for( block = mem_blocks[ k ].next; block != &mem_blocks[ k ]; block = block->next )
{
numBlocks++;
numBytes += block->size;
if( Cmd_Argc() != 1 ) {
//Com_Printf( "block: %p size: %i\n", block, block->size );
}
}
Com_Printf( "%zu bytes in %zu blocks in ", numBytes, numBlocks );
switch( k )
{
case TAG_GENERAL:
Com_Printf( "general memory pool.\n" );
break;
case TAG_BOTLIB:
Com_Printf( "botlib memory pool.\n" );
break;
case TAG_RENDERER:
Com_Printf( "renderer memory pool.\n" );
break;
case TAG_CGAME:
Com_Printf( "cgame memory pool.\n" );
break;
case TAG_GAME:
Com_Printf( "game memory pool.\n" );
break;
case TAG_CLIENT:
Com_Printf( "client memory pool.\n" );
break;
case TAG_TIKI:
Com_Printf( "tiki memory pool.\n" );
break;
case TAG_STRINGS_AND_COMMANDS:
Com_Printf( "strings and commands memory pool.\n" );
break;
case TAG_SOUND:
Com_Printf( "sound memory pool.\n" );
break;
case TAG_STATIC:
Com_Printf( "static memory pool.\n" );
break;
case TAG_STATIC_RENDERER:
Com_Printf( "static renderer memory pool.\n" );
break;
case TAG_SKEL:
Com_Printf( "skeletor memory pool.\n" );
break;
case TAG_TEMP:
Com_Printf( "temporary memory pool.\n" );
break;
case TAG_EXE:
Com_Printf( "exe memory pool.\n" );
break;
default:
Com_Error( ERR_FATAL, "Z_MemInfo: unknown memory pool\n" );
break;
}
totalBlocks += numBlocks;
totalBytes += numBytes;
}
Com_Printf( "\n%.2f Kbytes in %zu blocks in all memory pools\n", ( float )totalBytes / 1024.0f, totalBlocks );
Com_Printf( "\n%.2f megabytes in 'new' system memory\n", 1.024f );
#ifndef DEDICATED
if (re.CountTextureMemory) {
Com_Printf( "\n%.2f megabytes in texture memory\n", ( float )re.CountTextureMemory() / 1024.0f );
Com_Printf( "\n%.1f megabytes in total allocations\n", ( float )re.CountTextureMemory() + totalBytes - 1 / 1024.0f );
}
#endif
}
/*
========================
Z_InitMemory
========================
*/
void Z_InitMemory( void ) {
int k;
memset( &mem_blocks, 0, sizeof( mem_blocks ) );
for( k = 0; k < 16; k++ )
{
mem_blocks[ k ].prev = &mem_blocks[ k ];
mem_blocks[ k ].next = &mem_blocks[ k ];
}
Cmd_AddCommand( "meminfo", Z_Meminfo_f );
}
/*
========================
Z_Shutdown
========================
*/
void Z_Shutdown( void ) {
int k;
Z_CheckHeap();
for( k = 0; k < TAG_NUM_TOTAL_TAGS; k++ ) {
Z_FreeTags( k );
}
}
/*
=================
Hunk_Alloc
Allocate permanent (until the hunk is cleared) memory
=================
*/
#ifdef HUNK_DEBUG
void *Hunk_AllocDebug( int size, ha_pref preference, const char *label, const char *file, int line ) {
#else
void *Hunk_Alloc( int size, ha_pref preference ) {
#endif
void *ptr;
ptr = Z_TagMalloc( size, TAG_STATIC );
memset( ptr, 0, size );
return ptr;
}
/*
=================
Hunk_Clear
The server calls this before shutting down or loading a new map
=================
*/
void Hunk_Clear( void ) {
Z_FreeTags( TAG_STATIC );
}
/*
=================
Hunk_AllocateTempMemory
This is used by the file loading system.
Multiple files can be loaded in temporary memory.
When the files-in-use count reaches zero, all temp memory will be deleted
=================
*/
void *Hunk_AllocateTempMemory(int size ) {
return Z_TagMalloc( size, TAG_TEMP );
}
/*
========================
Hunk_FreeTempMemory
========================
*/
void Hunk_FreeTempMemory( void *ptr ) {
Z_Free( ptr );
}
/*
=================
Hunk_ClearTempMemory
The temp space is no longer needed. If we have left more
touched but unused memory on this side, have future
permanent allocs use this side.
=================
*/
void Hunk_ClearTempMemory( void ) {
Z_FreeTags( TAG_TEMP );
}
/*
===================
Hunk_SetMark
The server calls this after the level and game VM have been loaded
===================
*/
void Hunk_SetMark( void ) {
}
/*
===============
Com_TouchMemory
Touch all known used data to make sure it is paged in
===============
*/
void Com_TouchMemory( void ) {
Z_TouchMemory();
}
/*
=================
Com_InitHunkZoneMemory
=================
*/
void Com_InitHunkMemory( void ) {
}
/*
========================
Z_Malloc
========================
*/
#ifdef ZONE_DEBUG
void *Z_MallocDebug( int size, const char *label, const char *file, int line ) {
#else
void *Z_Malloc( int size ) {
#endif
void *buf;
//Z_CheckHeap (); // DEBUG
#ifdef ZONE_DEBUG
buf = Z_TagMallocDebug( size, TAG_GENERAL, label, file, line );
#else
buf = Z_TagMalloc( size, TAG_GENERAL );
#endif
Com_Memset( buf, 0, size );
return buf;
}
|