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