ccutil/memblk.cpp File Reference

#include "mfcpch.h"
#include <stdlib.h>
#include <string.h>
#include "stderr.h"
#include "memryerr.h"
#include "hashfn.h"
#include "tprintf.h"
#include "memry.h"
#include "memblk.h"
#include <signal.h>

Go to the source code of this file.

Classes

Defines

Functions

Variables


Define Documentation

#define EXTERN

Definition at line 56 of file memblk.cpp.


Function Documentation

void check_struct ( INT8  level,
INT32  count 
)

Check a particular structure size for consistency.

Definition at line 944 of file memblk.cpp.

References ABORT, BADSTRUCTCOUNT, ERRCODE::error(), free_structs, MAX_STRUCTS, MEMCHECKS, MEMTOOBIG, name_counts, NULL, owner_counts, owner_names, MEMUNION::ptr, STRUCT_BLOCK_SIZE, struct_blocks, structs_in_use, and tprintf().

Referenced by check_structs(), and free_struct().

00947                    {
00948   MEMUNION *element;             //current element
00949   MEMUNION *block;               //current block
00950   INT32 struct_count;            //no of required structs
00951   INT32 block_count;             //no of structure blocks
00952   INT32 free_count;              //size of freelist*/
00953   INT32 name_index;              //named holder
00954   INT32 named_total;             //total held by names
00955 
00956                                  //no of MEMUNIONS-1
00957   struct_count = (count - 1) / sizeof (MEMUNION);
00958   if (struct_count < 0 || struct_count >= MAX_STRUCTS)
00959                                  //request too big
00960     MEMTOOBIG.error ("check_struct", ABORT, "%d", (int) count);
00961 
00962   free_count = 0;                //size of freelist
00963                                  //count blocks
00964   for (block_count = 0, block = struct_blocks[struct_count]; block != NULL; block = block->ptr, block_count++);
00965   if (block_count > 0) {
00966                                  //scan freelist
00967     for (element = free_structs[struct_count]; element != NULL; element = element->ptr)
00968       free_count++;
00969     if (level >= MEMCHECKS) {
00970       tprintf ("No of structs of size %d in use=%d,",
00971         (int) count, (int) structs_in_use[struct_count]);
00972       tprintf (" %d free", free_count);
00973       tprintf (" in %d blocks, total space=%d\n",
00974         (int) block_count,
00975         block_count * STRUCT_BLOCK_SIZE * sizeof (MEMUNION));
00976       for (named_total = 0, name_index = 0;
00977       name_index < name_counts[struct_count]; name_index++) {
00978         tprintf ("No held by %s=%d\n",
00979           owner_names[struct_count][name_index],
00980           owner_counts[struct_count][name_index]);
00981         named_total += owner_counts[struct_count][name_index];
00982       }
00983       tprintf ("Total held by names=%d\n", named_total);
00984     }
00985   }
00986   if (structs_in_use[struct_count] + free_count
00987     != block_count * (STRUCT_BLOCK_SIZE / (struct_count + 1) - 1))
00988     BADSTRUCTCOUNT.error ("check_struct", ABORT, "%d+%d=%d",
00989       structs_in_use[struct_count], free_count,
00990       block_count * (STRUCT_BLOCK_SIZE /
00991       (struct_count + 1) - 1));
00992 }

void check_structs ( INT8  level  ) 

Reports statistics on each maintained structure type by calling free_struct(NULL) on each.

Only active structure types are reported.

Definition at line 1001 of file memblk.cpp.

References check_struct(), and MAX_STRUCTS.

Referenced by check_mem().

01003                     {
01004   INT8 index;                    //index to structs
01005 
01006   for (index = 1; index <= MAX_STRUCTS; index++)
01007                                  //check number allocated
01008     check_struct (level, index * sizeof (MEMUNION));
01009 }

INT32 identify_struct_owner ( INT32  struct_count,
const char *  name 
)

Get an index into the table of owners of structures.

Implemented very inefficiently, but only a debug tool!

Definition at line 922 of file memblk.cpp.

References MAX_CLASSES, name_counts, owner_counts, and owner_names.

Referenced by alloc_struct(), and free_struct().

00925                              {
00926   INT32 index;                   //index to structure
00927 
00928   for (index = 0; index < name_counts[struct_count]
00929     && strcmp (name, owner_names[struct_count][index]); index++);
00930   if (index < MAX_CLASSES) {
00931     if (index == name_counts[struct_count]) {
00932       name_counts[struct_count]++;
00933       owner_names[struct_count][index] = name;
00934       owner_counts[struct_count][index] = 0;
00935     }
00936   }
00937   return index;
00938 }

void* new_struct_block (  ) 

Allocate space for a new block of structures.

The space is obtained from alloc_mem, and a freelist of such blocks is maintained for when the individual structure types get completely freed.

Definition at line 1019 of file memblk.cpp.

References alloc_mem_p(), free_block, NULL, MEMUNION::ptr, and STRUCT_BLOCK_SIZE.

Referenced by alloc_struct().

01019                          {  //allocate memory
01020   MEMUNION *element;             //current element
01021   MEMUNION *returnelement;       //return value
01022 
01023   returnelement = free_block;
01024   if (returnelement == NULL) {
01025                                  //need a new block
01026     element =
01027       (MEMUNION *) alloc_mem_p (STRUCT_BLOCK_SIZE * sizeof (MEMUNION));
01028     if (element == NULL)
01029       return NULL;               //can't get more
01030     returnelement = element;     //going to return 1st
01031   }
01032   else {
01033                                  //new free one
01034     free_block = returnelement->ptr;
01035   }
01036   return returnelement;          //free cell
01037 }

void old_struct_block ( MEMUNION deadblock  ) 

Free memory allocated by new_struct_block.

The block is returned to a freelist ready for a new call to new_struct_block.

This saves confusion over freeing "permanent" blocks, yet allows them to be recycled for different structures.

Definition at line 1049 of file memblk.cpp.

References free_block, free_mem(), free_struct_blocks, MAX_FREE_S_BLOCKS, NULL, and MEMUNION::ptr.

Referenced by free_struct().

01051                        {
01052   if (deadblock != NULL) {
01053     deadblock->ptr = free_block; //add to freelist
01054     free_block = deadblock;
01055     free_struct_blocks++;
01056   }
01057   if (free_struct_blocks > MAX_FREE_S_BLOCKS) {
01058     MEMUNION *next_block;        //next in list
01059     deadblock = free_block;
01060     do {
01061       next_block = deadblock->ptr;
01062       free_mem(deadblock);  //really free it
01063       deadblock = next_block;
01064     }
01065     while (deadblock != NULL);
01066     free_struct_blocks = 0;
01067     free_block = NULL;
01068   }
01069 }

void* trace_caller ( INT32  depth  ) 

Return the return address of the caller at a given depth back.

0 gives the return address of the caller to trace_caller. S300 ONLY!!

Definition at line 861 of file memblk.cpp.

References UWREC::cur_frsize, UWREC::curdp, UWREC::currlo, UWREC::currls, UWREC::cursp, NULL, and UWREC::toprp.

Referenced by alloc_big_mem(), alloc_big_zeros(), alloc_mem(), alloc_mem_p(), free_big_mem(), free_mem(), and MEM_ALLOCATOR::new_block().

00863                     {
00864   #ifdef hp9000s800
00865 
00866   unsigned sp, pc, rp;           //registers
00867   UWREC rec1;                    //for unwinder
00868   UWREC rec2;
00869 
00870   sp = (unsigned) (&depth + 9);
00871   pc = *(int *) (sp - 20);
00872   rp = 0;
00873   get_pcspace(&rec1, pc);
00874   rec1.cur_frsize = 0xc0;
00875   rec1.currlo = pc & ~3;
00876   rec1.curdp = 0;
00877   rec1.toprp = rp;
00878 
00879   while (depth > 0) {
00880     if (U_get_previous_frame (&rec1, &rec2))
00881       return NULL;
00882     rec1.currlo = rec2.currlo;
00883     rec1.cur_frsize = rec2.cur_frsize;
00884     rec1.cursp = rec2.cursp;
00885     rec1.currls = rec2.currls;
00886     rec1.curdp = rec2.curdp;
00887     depth--;
00888   }
00889   return (void *) rec1.currlo;
00890   #else
00891   void *a6;                      //address register
00892 
00893   a6 = &depth - 2;
00894   while (depth > 0) {
00895     a6 = *(void **) a6;          //follow chain
00896     depth--;
00897   }
00898   return *((void **) a6 + 1);
00899   #endif
00900 }


Variable Documentation

EXTERN MEM_ALLOCATOR big_mem

Definition at line 58 of file memblk.cpp.

Referenced by alloc_big_mem(), alloc_big_zeros(), check_mem(), and free_big_mem().

EXTERN INT32 blocks_in_use[MAX_STRUCTS]

Definition at line 65 of file memblk.cpp.

Referenced by alloc_struct(), and free_struct().

MEMUNION* free_block = NULL

Definition at line 54 of file memblk.cpp.

Referenced by new_struct_block(), and old_struct_block().

EXTERN INT32 free_struct_blocks

Definition at line 72 of file memblk.cpp.

Referenced by old_struct_block().

EXTERN MEMUNION* free_structs[MAX_STRUCTS]

Definition at line 61 of file memblk.cpp.

Referenced by alloc_struct(), check_struct(), and free_struct().

EXTERN MEM_ALLOCATOR main_mem

Definition at line 59 of file memblk.cpp.

Referenced by alloc_mem(), alloc_mem_p(), check_mem(), free_big_mem(), and free_mem().

EXTERN INT16 name_counts[MAX_STRUCTS]

Definition at line 71 of file memblk.cpp.

Referenced by check_struct(), and identify_struct_owner().

EXTERN INT32 owner_counts[MAX_STRUCTS][MAX_CLASSES]

Definition at line 69 of file memblk.cpp.

Referenced by alloc_struct(), check_struct(), free_struct(), and identify_struct_owner().

EXTERN const char* owner_names[MAX_STRUCTS][MAX_CLASSES]

Definition at line 68 of file memblk.cpp.

Referenced by check_struct(), and identify_struct_owner().

EXTERN MEMUNION* struct_blocks[MAX_STRUCTS]

Definition at line 67 of file memblk.cpp.

Referenced by alloc_struct(), check_struct(), and free_struct().

EXTERN INT32 structs_in_use[MAX_STRUCTS]

Definition at line 63 of file memblk.cpp.

Referenced by alloc_struct(), check_struct(), and free_struct().


Generated on Wed Feb 28 19:49:17 2007 for Tesseract by  doxygen 1.5.1