📄 memry.cpp
字号:
/********************************************************************** * File: memry.c (Formerly memory.c) * Description: Memory allocation with builtin safety checks. * Author: Ray Smith * Created: Wed Jan 22 09:43:33 GMT 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. * **********************************************************************/#include "mfcpch.h"#include <stdlib.h>#ifdef __UNIX__#include <assert.h>#endif#include "stderr.h"#include "tprintf.h"#include "memblk.h"#include "memry.h"//#define COUNTING_CLASS_STRUCTURES/********************************************************************** * new * * Replace global new to get at memory leaks etc. **********************************************************************//*void* operator new( //allocate memorysize_t size //amount to allocate){ if (size==0) { err.log(RESULT_LOGICAL_ERROR,E_LOC,ERR_PRIMITIVES, ERR_SCROLLING,ERR_CONTINUE,ERR_ERROR, "Zero requested of new"); size=1; } return alloc_big_mem(size);}void operator delete( //free memoryvoid* addr //mem to free){ free_big_mem(addr);}*//********************************************************************** * check_mem * * Check consistency of all memory controlled by alloc_mem. **********************************************************************/DLLSYM void check_mem( //check consistency const char *string, //context message INT8 level //level of check ) { big_mem.check (string, level); main_mem.check (string, level); check_structs(level);}/********************************************************************** * alloc_string * * Allocate space for a string. The space can only be used for chars as * it is not aligned. Allocation is guaranteed to be fast and not cause * fragmentation for small strings (upto 10*worst alignment). Calls for * larger strings will be satisfied with alloc_mem. * Use free_string to free the space from alloc_string. **********************************************************************/DLLSYM char *alloc_string( //allocate string INT32 count //no of chars required ) {#ifdef RAYS_MALLOC char *string; //allocated string if (count < 1 || count > MAX_CHUNK) { tprintf ("Invalid size %d requested of alloc_string", count); return NULL; } count++; //add size byte if (count <= MAX_STRUCTS * sizeof (MEMUNION)) { string = (char *) alloc_struct (count, "alloc_string"); //get a fast structure if (string == NULL) { tprintf ("No memory for alloc_string"); return NULL; } string[0] = (INT8) count; //save its length } else { //get a big block string = (char *) alloc_mem (count); if (string == NULL) { tprintf ("No memory for alloc_string"); return NULL; } string[0] = 0; //mark its id } return &string[1]; //string for user#else return static_cast<char*>(malloc(count));#endif}/********************************************************************** * free_string * * Free a string allocated by alloc_string. **********************************************************************/DLLSYM void free_string( //free a string char *string //string to free ) {#ifdef RAYS_MALLOC if (((ptrdiff_t) string & 3) == 1) { //one over word string--; //get id marker if (*string == 0) { free_mem(string); //generally free it return; } else if (*string <= MAX_STRUCTS * sizeof (MEMUNION)) { //free structure free_struct (string, *string, "alloc_string"); return; } } tprintf ("Non-string given to free_string");#else free(string);#endif}/********************************************************************** * alloc_struct * * Allocate space for a structure. This function is designed to be * fast and fragmentation free for arbitrary combinations of small * objects. (Upto 40 bytes in length.) * It can be used for any size of object up to 512K, but you must use * free_struct to release the memory it gives. alloc_mem is better * for arbitrary data blocks of large size (>40 bytes.) * alloc_struct always aborts if the allocation fails. **********************************************************************/DLLSYM void *alloc_struct ( //allocate memoryINT32 count, //no of chars required#if defined COUNTING_CLASS_STRUCTURESconst char *name //name of type#elseconst char * //name of type#endif) {#ifdef RAYS_MALLOC MEMUNION *element; //current element MEMUNION *returnelement; //return value INT32 struct_count; //no of required structs INT32 blocksize; //no of structs in block INT32 index; //index to structure if (count < 1 || count > MAX_CHUNK) { tprintf ("Invalid size %d requested of alloc_struct", count); return NULL; } // tprintf("Allocating structure of size %d\n",count); //no of MEMUNIONS-1 struct_count = (count - 1) / sizeof (MEMUNION); if (struct_count < MAX_STRUCTS) { //can do fixed sizes #ifdef COUNTING_CLASS_STRUCTURES if (name != NULL) { index = identify_struct_owner (struct_count, name); if (index < MAX_CLASSES) owner_counts[struct_count][index]++; } #endif //head of freelist returnelement = free_structs[struct_count]; if (returnelement == NULL) { //need a new block //get one element = (MEMUNION *) new_struct_block (); if (element == NULL) { tprintf ("No memory to satisfy request for %d", (int) count); return NULL; } //add to block list element->ptr = struct_blocks[struct_count]; struct_blocks[struct_count] = element; blocks_in_use[struct_count]++; element++; //free cell returnelement = element; //going to return 1st blocksize = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; for (index = 1; index < blocksize; index++) { //make links element->ptr = element + struct_count + 1; element += struct_count + 1; } element->ptr = NULL; //end of freelist } //new free one free_structs[struct_count] = returnelement->ptr; //count number issued structs_in_use[struct_count]++; } else { //just get some returnelement = (MEMUNION *) alloc_mem (count); if (returnelement == NULL) { tprintf ("No memory to satisfy request for %d", (int) count); return NULL; } } return returnelement; //free cell#else return malloc(count);#endif}/********************************************************************** * free_struct * * Free memory allocated by alloc_struct. The size must be supplied. **********************************************************************/DLLSYM voidfree_struct ( //free a structurevoid *deadstruct, //structure to freeINT32 count, //no of bytes#if defined COUNTING_CLASS_STRUCTURESconst char *name //name of type#elseconst char * //name of type#endif) {#ifdef RAYS_MALLOC MEMUNION *end_element; //current element MEMUNION *element; //current element MEMUNION *prev_element; //previous element MEMUNION *prev_block; //previous element MEMUNION *nextblock; //next block in list MEMUNION *block; //next block in list INT32 struct_count; //no of required structs INT32 index; //to structure counts
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -