f3e15b8c8d
- aggiunti file della libreria e progetto visual studio.
232 lines
6.9 KiB
C++
232 lines
6.9 KiB
C++
/*****************************************************************************/
|
|
/* */
|
|
/* Copyright (C) 1996-2023 M. Held */
|
|
/* */
|
|
/* This code is not in the public domain. All rights reserved! Please make */
|
|
/* sure to read the full copyright statement contained in "README.txt" or in */
|
|
/* the "main" file of this code, such as "main.cc". */
|
|
/* */
|
|
/*****************************************************************************/
|
|
/* */
|
|
/* Written by: Martin Held */
|
|
/* */
|
|
/* E-Mail: held@cs.sbg.ac.at */
|
|
/* Fax Mail: (+43 662) 8044-611 */
|
|
/* Voice Mail: (+43 662) 8044-6304 */
|
|
/* Snail Mail: Martin Held */
|
|
/* FB Informatik */
|
|
/* Universitaet Salzburg */
|
|
/* A-5020 Salzburg, Austria */
|
|
/* */
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
/* get standard libraries */
|
|
/* */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
#include <iostream>
|
|
|
|
/* */
|
|
/* get my header files */
|
|
/* */
|
|
#include "fpkernel.h"
|
|
#include "vronivector.h"
|
|
#include "vroni_object.h"
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
void * vroniObject::ReallocateArray(void *old_ptr, int number, size_t size,
|
|
char const *var_name)
|
|
{
|
|
void *new_ptr;
|
|
|
|
if (old_ptr != (void*)NULL) {
|
|
#ifdef DEBUG_MEMORY
|
|
UpdateMemoryStatus(old_ptr, mem_reallocated, size, number);
|
|
#endif /* DEBUG_MEMORY */
|
|
new_ptr = (void*) realloc((void*) old_ptr, number * size);
|
|
if (new_ptr == (void*)NULL) {
|
|
fprintf(stderr,
|
|
"ReallocateArray() - Cannot get %d elements of %d bytes for array `%s'.\n",
|
|
number, (int) size, var_name);
|
|
throw std::runtime_error("VRONI error: ReallocateArray() - memory reallocation failed!");
|
|
}
|
|
}
|
|
else {
|
|
new_ptr = (void*) calloc(number, size);
|
|
if (new_ptr == (void*)NULL) {
|
|
fprintf(stderr,
|
|
"ReallocateArray() - Cannot get %d elements of %d bytes for array `%s'.\n",
|
|
number, (int) size, var_name);
|
|
throw std::runtime_error("VRONI error: ReallocateArray() - memory allocation failed!");
|
|
}
|
|
}
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
UpdateMemoryStatus(new_ptr, mem_allocated, size, number);
|
|
#endif /* DEBUG_MEMORY */
|
|
|
|
return new_ptr;
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
void vroniObject::UpdateMemoryStatus(void *ptr, memory_status status,
|
|
size_t size, unsigned int high)
|
|
{
|
|
int i;
|
|
|
|
switch(status) {
|
|
case(mem_allocated):
|
|
if (memory_dbg_cursor < MAX_MEMORY_CURSOR) {
|
|
memory_history[memory_dbg_cursor].array_ptr = ptr;
|
|
memory_history[memory_dbg_cursor].size = size;
|
|
memory_history[memory_dbg_cursor].high = high;
|
|
++memory_dbg_cursor;
|
|
curr_memory += size * high;
|
|
if (curr_memory > max_memory) max_memory = curr_memory;
|
|
}
|
|
else {
|
|
fprintf(stderr,
|
|
"UpdateMemoryStatus: too many memory allocations\n");
|
|
}
|
|
break;
|
|
case(mem_reallocated):
|
|
if (ptr != NULL) {
|
|
i = SearchMemoryHistory(ptr);
|
|
if (i >= 0) {
|
|
if (memory_history[i].size != size) {
|
|
fprintf(stderr,
|
|
"UpdateMemoryStatus: size of realloc elements ");
|
|
fprintf(stderr, "does not match orginal size!***\n");
|
|
}
|
|
curr_memory -= size * memory_history[i].high;
|
|
memory_history[i].high = 0;
|
|
}
|
|
else {
|
|
fprintf(stderr,
|
|
"UpdateMemoryStatus: memory allocation and ");
|
|
fprintf(stderr, "deallocation is messed up!\n");
|
|
}
|
|
}
|
|
break;
|
|
case(mem_freed):
|
|
i = SearchMemoryHistory(ptr);
|
|
if (i >= 0) {
|
|
curr_memory -= memory_history[i].size * memory_history[i].high;
|
|
memory_history[i].high = 0;
|
|
}
|
|
else
|
|
fprintf(stderr,
|
|
"UpdateMemoryStatus: memory allocation and deallocation is messed up!\n");
|
|
break;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
int vroniObject::SearchMemoryHistory(void *ptr)
|
|
{
|
|
int i;
|
|
|
|
i = memory_dbg_cursor - 1;
|
|
|
|
while (i >= 0) {
|
|
if (memory_history[i].array_ptr == ptr) return i;
|
|
--i;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
#endif /* DEBUG_MEMORY */
|
|
|
|
|
|
|
|
unsigned long vroniObject::ReportMaxNumberBytes(void)
|
|
{
|
|
#ifdef DEBUG_MEMORY
|
|
return max_memory;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
unsigned long vroniObject::ReportCurrNumberBytes(void)
|
|
{
|
|
#ifdef DEBUG_MEMORY
|
|
return curr_memory;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
vr_bool vroniObject::IndexOutOfBounds(void *array_ptr, char const *var_name, size_t size,
|
|
int index)
|
|
{
|
|
int i;
|
|
|
|
i = SearchMemoryHistory(array_ptr);
|
|
if (i >= 0) {
|
|
if (size != memory_history[i].size) {
|
|
fprintf(stderr,
|
|
"IndexOutOfBounds() - Bytes per component of `%s[]' do not match bytes ",
|
|
var_name);
|
|
fprintf(stderr, "specified!\n");
|
|
fprintf(stderr,
|
|
" Bytes per component: %d bytes; bytes specified: %d\n",
|
|
memory_history[i].size, size);
|
|
}
|
|
return ((index < 0) || (index >= (int) memory_history[i].high));
|
|
}
|
|
else {
|
|
fprintf(stderr, "IndexOutOfBounds() - `%s[]' has already been ",
|
|
var_name);
|
|
fprintf(stderr, "deallocated!\n");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
#endif /* DEBUG_MEMORY */
|
|
|
|
|
|
|
|
void vroniObject::FreeMemory(void **ptr, char const *var_name)
|
|
{
|
|
if (*ptr == NULL) return;
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
UpdateMemoryStatus(*ptr, mem_freed, 0, 0);
|
|
#endif /* DEBUG_MEMORY */
|
|
|
|
free(*ptr);
|
|
*ptr = NULL;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
vr_bool vroniObject::AllMemoryFreed()
|
|
{
|
|
#ifdef DEBUG_MEMORY
|
|
int i;
|
|
|
|
for (i = 0; i < memory_dbg_cursor; ++i) {
|
|
if (memory_history[i].high != 0) return false;
|
|
}
|
|
#endif /* DEBUG_MEMORY */
|
|
|
|
return true;
|
|
}
|