X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/33a9da85867c540b95d99573defe39b47c5f6f45..daf9d10c7877d35670e36e52be8a5207706d3bb1:/src/xbt/mmalloc/mmcheck.c diff --git a/src/xbt/mmalloc/mmcheck.c b/src/xbt/mmalloc/mmcheck.c index 48936b3e63..46b48a6aec 100644 --- a/src/xbt/mmalloc/mmcheck.c +++ b/src/xbt/mmalloc/mmcheck.c @@ -2,25 +2,13 @@ Copyright 1990, 1991, 1992 Free Software Foundation Written May 1989 by Mike Haertel. - Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com) + Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com) */ -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. +/* Copyright (c) 2010. The SimGrid Team. + * All rights reserved. */ -The GNU C Library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. - - The author may be reached (Email) at the address mike@ai.mit.edu, - or (US mail) as Mike Haertel c/o Free Software Foundation. */ +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ #include "mmprivate.h" @@ -28,12 +16,12 @@ Boston, MA 02111-1307, USA. can specify an alternate function to be called instead (and probably will want to). */ -extern void abort PARAMS ((void)); +extern void abort(void); /* Arbitrary magical numbers. */ -#define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */ -#define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */ +#define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */ +#define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */ #define MAGICBYTE ((char) 0xd7) /* Each memory allocation is bounded by a header structure and a trailer @@ -45,99 +33,82 @@ extern void abort PARAMS ((void)); user's allocation area. The magic word can be tested to detect buffer underruns and the magic byte can be tested to detect overruns. */ -struct hdr - { - size_t size; /* Exact size requested by user. */ - unsigned long int magic; /* Magic number to check header integrity. */ - }; +struct hdr { + size_t size; /* Exact size requested by user. */ + unsigned long int magic; /* Magic number to check header integrity. */ +}; -static void checkhdr PARAMS ((struct mdesc *, CONST struct hdr *)); -static void mfree_check PARAMS ((PTR, PTR)); -static PTR mmalloc_check PARAMS ((PTR, size_t)); -static PTR mrealloc_check PARAMS ((PTR, PTR, size_t)); +static void checkhdr(struct mdesc *mdp, const struct hdr *hdr); +static void mfree_check(void *md, void *ptr); +static void *mmalloc_check(void *md, size_t size); +static void *mrealloc_check(void *md, void *ptr, size_t size); /* Check the magicword and magicbyte, and if either is corrupted then call the emergency abort function specified for the heap in use. */ -static void -checkhdr (mdp, hdr) - struct mdesc *mdp; - CONST struct hdr *hdr; +static void checkhdr(struct mdesc *mdp, const struct hdr *hdr) { - if (hdr -> magic != MAGICWORD || - ((char *) &hdr[1])[hdr -> size] != MAGICBYTE) - { - (*mdp -> abortfunc)(); - } + if (hdr->magic != MAGICWORD || + ((char *) &hdr[1])[hdr->size] != MAGICBYTE) { + (*mdp->abortfunc) (); + } } -static void -mfree_check (md, ptr) - PTR md; - PTR ptr; +static void mfree_check(void *md, void *ptr) { struct hdr *hdr = ((struct hdr *) ptr) - 1; struct mdesc *mdp; - mdp = MD_TO_MDP (md); - checkhdr (mdp, hdr); - hdr -> magic = MAGICWORDFREE; - mdp -> mfree_hook = NULL; - mfree (md, (PTR)hdr); - mdp -> mfree_hook = mfree_check; + mdp = MD_TO_MDP(md); + checkhdr(mdp, hdr); + hdr->magic = MAGICWORDFREE; + mdp->mfree_hook = NULL; + mfree(md, (void *) hdr); + mdp->mfree_hook = mfree_check; } -static PTR -mmalloc_check (md, size) - PTR md; - size_t size; +static void *mmalloc_check(void *md, size_t size) { struct hdr *hdr; struct mdesc *mdp; size_t nbytes; - mdp = MD_TO_MDP (md); - mdp -> mmalloc_hook = NULL; - nbytes = sizeof (struct hdr) + size + 1; - hdr = (struct hdr *) mmalloc (md, nbytes); - mdp -> mmalloc_hook = mmalloc_check; - if (hdr != NULL) - { - hdr -> size = size; - hdr -> magic = MAGICWORD; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((PTR) hdr); + mdp = MD_TO_MDP(md); + mdp->mmalloc_hook = NULL; + nbytes = sizeof(struct hdr) + size + 1; + hdr = (struct hdr *) mmalloc(md, nbytes); + mdp->mmalloc_hook = mmalloc_check; + if (hdr != NULL) { + hdr->size = size; + hdr->magic = MAGICWORD; + hdr++; + *((char *) hdr + size) = MAGICBYTE; + } + return ((void *) hdr); } -static PTR -mrealloc_check (md, ptr, size) - PTR md; - PTR ptr; - size_t size; +static void *mrealloc_check(void *md, void *ptr, size_t size) { struct hdr *hdr = ((struct hdr *) ptr) - 1; struct mdesc *mdp; size_t nbytes; - mdp = MD_TO_MDP (md); - checkhdr (mdp, hdr); - mdp -> mfree_hook = NULL; - mdp -> mmalloc_hook = NULL; - mdp -> mrealloc_hook = NULL; - nbytes = sizeof (struct hdr) + size + 1; - hdr = (struct hdr *) mrealloc (md, (PTR) hdr, nbytes); - mdp -> mfree_hook = mfree_check; - mdp -> mmalloc_hook = mmalloc_check; - mdp -> mrealloc_hook = mrealloc_check; - if (hdr != NULL) - { - hdr -> size = size; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((PTR) hdr); + mdp = MD_TO_MDP(md); + checkhdr(mdp, hdr); + mdp->mfree_hook = NULL; + mdp->mmalloc_hook = NULL; + mdp->mrealloc_hook = NULL; + nbytes = sizeof(struct hdr) + size + 1; + hdr = (struct hdr *) mrealloc(md, (void *) hdr, nbytes); + mdp->mfree_hook = mfree_check; + mdp->mmalloc_hook = mmalloc_check; + mdp->mrealloc_hook = mrealloc_check; + if (hdr != NULL) { + hdr->size = size; + hdr++; + *((char *) hdr + size) = MAGICBYTE; + } + return ((void *) hdr); } /* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified @@ -170,40 +141,32 @@ mrealloc_check (md, ptr, size) Returns non-zero if checking is successfully enabled, zero otherwise. */ -int -mmcheckf (md, func, force) - PTR md; - void (*func) PARAMS ((void)); - int force; +int mmcheckf(void *md, void (*func) (void), int force) { struct mdesc *mdp; int rtnval; - mdp = MD_TO_MDP (md); + mdp = MD_TO_MDP(md); /* We can safely set or update the abort function at any time, regardless of whether or not we successfully do anything else. */ - mdp -> abortfunc = (func != NULL ? func : abort); + mdp->abortfunc = (func != NULL ? func : abort); /* If we haven't yet called mmalloc the first time for this heap, or if we have hooks that were previously installed, then allow the hooks to be initialized or updated. */ if (force || - !(mdp -> flags & MMALLOC_INITIALIZED) || - (mdp -> mfree_hook != NULL)) - { - mdp -> mfree_hook = mfree_check; - mdp -> mmalloc_hook = mmalloc_check; - mdp -> mrealloc_hook = mrealloc_check; - mdp -> flags |= MMALLOC_MMCHECK_USED; - rtnval = 1; - } - else - { - rtnval = 0; - } + !(mdp->flags & MMALLOC_INITIALIZED) || (mdp->mfree_hook != NULL)) { + mdp->mfree_hook = mfree_check; + mdp->mmalloc_hook = mmalloc_check; + mdp->mrealloc_hook = mrealloc_check; + mdp->flags |= MMALLOC_MMCHECK_USED; + rtnval = 1; + } else { + rtnval = 0; + } return (rtnval); } @@ -211,13 +174,10 @@ mmcheckf (md, func, force) /* This routine is for backwards compatibility only, in case there are still callers to the original mmcheck function. */ -int -mmcheck (md, func) - PTR md; - void (*func) PARAMS ((void)); +int mmcheck(void *md, void (*func) (void)) { int rtnval; - rtnval = mmcheckf (md, func, 0); + rtnval = mmcheckf(md, func, 0); return (rtnval); }