alpar@1: /* glpenv05.c (memory allocation) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: /* some processors need data to be properly aligned; the macro alpar@1: align_datasize enlarges the specified size of a data item to provide alpar@1: a proper alignment of immediately following data */ alpar@1: alpar@1: #define align_datasize(size) ((((size) + 15) / 16) * 16) alpar@1: /* 16 bytes is sufficient in both 32- and 64-bit environments alpar@1: (8 bytes is not sufficient in 64-bit environment due to jmp_buf) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_malloc - allocate memory block alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void *glp_malloc(int size); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_malloc allocates a memory block of size bytes long. alpar@1: * alpar@1: * Note that being allocated the memory block contains arbitrary data alpar@1: * (not binary zeros). alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_malloc returns a pointer to the allocated block. alpar@1: * To free this block the routine glp_free (not free!) must be used. */ alpar@1: alpar@1: void *glp_malloc(int size) alpar@1: { ENV *env = get_env_ptr(); alpar@1: MEM *desc; alpar@1: int size_of_desc = align_datasize(sizeof(MEM)); alpar@1: if (size < 1 || size > INT_MAX - size_of_desc) alpar@1: xerror("glp_malloc: size = %d; invalid parameter\n", size); alpar@1: size += size_of_desc; alpar@1: if (xlcmp(xlset(size), alpar@1: xlsub(env->mem_limit, env->mem_total)) > 0) alpar@1: xerror("glp_malloc: memory limit exceeded\n"); alpar@1: if (env->mem_count == INT_MAX) alpar@1: xerror("glp_malloc: too many memory blocks allocated\n"); alpar@1: desc = malloc(size); alpar@1: if (desc == NULL) alpar@1: xerror("glp_malloc: no memory available\n"); alpar@1: memset(desc, '?', size); alpar@1: desc->flag = MEM_MAGIC; alpar@1: desc->size = size; alpar@1: desc->prev = NULL; alpar@1: desc->next = env->mem_ptr; alpar@1: if (desc->next != NULL) desc->next->prev = desc; alpar@1: env->mem_ptr = desc; alpar@1: env->mem_count++; alpar@1: if (env->mem_cpeak < env->mem_count) alpar@1: env->mem_cpeak = env->mem_count; alpar@1: env->mem_total = xladd(env->mem_total, xlset(size)); alpar@1: if (xlcmp(env->mem_tpeak, env->mem_total) < 0) alpar@1: env->mem_tpeak = env->mem_total; alpar@1: return (void *)((char *)desc + size_of_desc); alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_calloc - allocate memory block alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void *glp_calloc(int n, int size); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_calloc allocates a memory block of (n*size) bytes alpar@1: * long. alpar@1: * alpar@1: * Note that being allocated the memory block contains arbitrary data alpar@1: * (not binary zeros). alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_calloc returns a pointer to the allocated block. alpar@1: * To free this block the routine glp_free (not free!) must be used. */ alpar@1: alpar@1: void *glp_calloc(int n, int size) alpar@1: { if (n < 1) alpar@1: xerror("glp_calloc: n = %d; invalid parameter\n", n); alpar@1: if (size < 1) alpar@1: xerror("glp_calloc: size = %d; invalid parameter\n", size); alpar@1: if (n > INT_MAX / size) alpar@1: xerror("glp_calloc: n = %d; size = %d; array too big\n", n, alpar@1: size); alpar@1: return xmalloc(n * size); alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_free - free memory block alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_free(void *ptr); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_free frees a memory block pointed to by ptr, which alpar@1: * was previuosly allocated by the routine glp_malloc or glp_calloc. */ alpar@1: alpar@1: void glp_free(void *ptr) alpar@1: { ENV *env = get_env_ptr(); alpar@1: MEM *desc; alpar@1: int size_of_desc = align_datasize(sizeof(MEM)); alpar@1: if (ptr == NULL) alpar@1: xerror("glp_free: ptr = %p; null pointer\n", ptr); alpar@1: desc = (void *)((char *)ptr - size_of_desc); alpar@1: if (desc->flag != MEM_MAGIC) alpar@1: xerror("glp_free: ptr = %p; invalid pointer\n", ptr); alpar@1: if (env->mem_count == 0 || alpar@1: xlcmp(env->mem_total, xlset(desc->size)) < 0) alpar@1: xerror("glp_free: memory allocation error\n"); alpar@1: if (desc->prev == NULL) alpar@1: env->mem_ptr = desc->next; alpar@1: else alpar@1: desc->prev->next = desc->next; alpar@1: if (desc->next == NULL) alpar@1: ; alpar@1: else alpar@1: desc->next->prev = desc->prev; alpar@1: env->mem_count--; alpar@1: env->mem_total = xlsub(env->mem_total, xlset(desc->size)); alpar@1: memset(desc, '?', size_of_desc); alpar@1: free(desc); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_mem_limit - set memory usage limit alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_mem_limit(int limit); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_mem_limit limits the amount of memory available for alpar@1: * dynamic allocation (in GLPK routines) to limit megabytes. */ alpar@1: alpar@1: void glp_mem_limit(int limit) alpar@1: { ENV *env = get_env_ptr(); alpar@1: if (limit < 0) alpar@1: xerror("glp_mem_limit: limit = %d; invalid parameter\n", alpar@1: limit); alpar@1: env->mem_limit = xlmul(xlset(limit), xlset(1 << 20)); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_mem_usage - get memory usage information alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_mem_usage(int *count, int *cpeak, glp_long *total, alpar@1: * glp_long *tpeak); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_mem_usage reports some information about utilization alpar@1: * of the memory by GLPK routines. Information is stored to locations alpar@1: * specified by corresponding parameters (see below). Any parameter can alpar@1: * be specified as NULL, in which case corresponding information is not alpar@1: * stored. alpar@1: * alpar@1: * *count is the number of the memory blocks currently allocated by the alpar@1: * routines xmalloc and xcalloc (one call to xmalloc or xcalloc results alpar@1: * in allocating one memory block). alpar@1: * alpar@1: * *cpeak is the peak value of *count reached since the initialization alpar@1: * of the GLPK library environment. alpar@1: * alpar@1: * *total is the total amount, in bytes, of the memory blocks currently alpar@1: * allocated by the routines xmalloc and xcalloc. alpar@1: * alpar@1: * *tpeak is the peak value of *total reached since the initialization alpar@1: * of the GLPK library envirionment. */ alpar@1: alpar@1: void glp_mem_usage(int *count, int *cpeak, glp_long *total, alpar@1: glp_long *tpeak) alpar@1: { ENV *env = get_env_ptr(); alpar@1: if (count != NULL) *count = env->mem_count; alpar@1: if (cpeak != NULL) *cpeak = env->mem_cpeak; alpar@1: if (total != NULL) *total = env->mem_total; alpar@1: if (tpeak != NULL) *tpeak = env->mem_tpeak; alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */