⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vex_util.c

📁 unix下调试内存泄露的工具源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------*//*---                                                         ---*//*--- This file (vex_util.c) is                               ---*//*--- Copyright (C) OpenWorks LLP.  All rights reserved.      ---*//*---                                                         ---*//*---------------------------------------------------------------*//*   This file is part of LibVEX, a library for dynamic binary   instrumentation and translation.   Copyright (C) 2004-2005 OpenWorks LLP.  All rights reserved.   This library is made available under a dual licensing scheme.   If you link LibVEX against other code all of which is itself   licensed under the GNU General Public License, version 2 dated June   1991 ("GPL v2"), then you may use LibVEX under the terms of the GPL   v2, as appearing in the file LICENSE.GPL.  If the file LICENSE.GPL   is missing, you can obtain a copy of the GPL v2 from the Free   Software Foundation Inc., 51 Franklin St, Fifth Floor, Boston, MA   02110-1301, USA.   For any other uses of LibVEX, you must first obtain a commercial   license from OpenWorks LLP.  Please contact info@open-works.co.uk   for information about commercial licensing.   This software is provided by OpenWorks LLP "as is" and any express   or implied warranties, including, but not limited to, the implied   warranties of merchantability and fitness for a particular purpose   are disclaimed.  In no event shall OpenWorks LLP be liable for any   direct, indirect, incidental, special, exemplary, or consequential   damages (including, but not limited to, procurement of substitute   goods or services; loss of use, data, or profits; or business   interruption) however caused and on any theory of liability,   whether in contract, strict liability, or tort (including   negligence or otherwise) arising in any way out of the use of this   software, even if advised of the possibility of such damage.   Neither the names of the U.S. Department of Energy nor the   University of California nor the names of its contributors may be   used to endorse or promote products derived from this software   without prior written permission.*/#include "libvex_basictypes.h"#include "libvex.h"#include "vex_globals.h"#include "vex_util.h"/*---------------------------------------------------------*//*--- Storage                                           ---*//*---------------------------------------------------------*//* Try to keep this as low as possible -- in particular, less than the   size of the smallest L2 cache we might encounter.  At 50000, my VIA   Nehemiah 1 GHz (a weedy machine) can satisfy 27 million calls/   second to LibVEX_Alloc(16) -- that is, allocate memory at over 400   MByte/sec.  Once the size increases enough to fall out of the cache   into memory, the rate falls by about a factor of 3. */#define N_TEMPORARY_BYTES 2400000static Char temporary[N_TEMPORARY_BYTES];static Int  temporary_used = 0;#define N_PERMANENT_BYTES 1000static Char permanent[N_TEMPORARY_BYTES];static Int  permanent_used = 0;/* Gather statistics. */static Int temporary_bytes_allocd = 0;static Int temporary_count_allocs = 0;static ULong temporary_bytes_allocd_TOT = 0;static ULong temporary_count_allocs_TOT = 0;/* The current allocation mode. */static VexAllocMode mode = VexAllocModeTEMP;void vexSetAllocMode ( VexAllocMode m ){   mode = m;}VexAllocMode vexGetAllocMode ( void ){   return mode;}/* Exported to library client. */void* LibVEX_Alloc ( Int nbytes ) {   /* 3 or 7 depending on host word size. */#  define ALIGN (sizeof(void*)-1)   vassert(vex_initdone);   vassert(nbytes >= 0);   if (vex_valgrind_support) {      /* ugly hack -- do not remove */      extern void* malloc ( int );      return malloc(nbytes);   } else {      nbytes = (nbytes + ALIGN) & ~ALIGN;      if (mode == VexAllocModeTEMP) {         if (temporary_used + nbytes > N_TEMPORARY_BYTES)            vpanic("VEX temporary storage exhausted.\n"                   "Increase N_TEMPORARY_BYTES and recompile.");         temporary_count_allocs++;         temporary_bytes_allocd += nbytes;         temporary_used += nbytes;         return (void*)(&temporary[temporary_used - nbytes]);      } else {         if (permanent_used + nbytes > N_PERMANENT_BYTES)            vpanic("VEX permanent storage exhausted.\n"                   "Increase N_PERMANENT_BYTES and recompile.");         permanent_used += nbytes;         return (void*)(&permanent[permanent_used - nbytes]);      }   }#  undef ALIGN}void vexClearTEMP ( void ){   /* vassert(vex_initdone); */ /* causes infinite assert loops */   temporary_bytes_allocd_TOT += (ULong)temporary_bytes_allocd;   temporary_count_allocs_TOT += (ULong)temporary_count_allocs;   temporary_used = 0;   temporary_bytes_allocd = 0;   temporary_count_allocs = 0;}/* Exported to library client. */void LibVEX_ShowAllocStats ( void ){   vex_printf("vex storage:  P %d,  T total %lld (%lld),  T curr %d (%d)\n",              permanent_used,              (Long)temporary_bytes_allocd_TOT,               (Long)temporary_count_allocs_TOT,              temporary_bytes_allocd, temporary_count_allocs );}/*---------------------------------------------------------*//*--- Bombing out                                       ---*//*---------------------------------------------------------*/__attribute__ ((noreturn))void vex_assert_fail ( const HChar* expr,                       const HChar* file, Int line, const HChar* fn ){   vex_printf( "\nvex: %s:%d (%s): Assertion `%s' failed.\n",               file, line, fn, expr );   (*vex_failure_exit)();}__attribute__ ((noreturn))void vpanic ( HChar* str ){   vex_printf("\nvex: the `impossible' happened:\n   %s\n", str);   (*vex_failure_exit)();}/*---------------------------------------------------------*//*--- vex_printf                                        ---*//*---------------------------------------------------------*//* This should be the only <...> include in the entire VEX library.   New code for vex_util.c should go above this point. */#include <stdarg.h>/* ---------------------------------------------------------------------   printf implementation.  The key function, vg_vprintf(), emits chars    into a caller-supplied function.  Distantly derived from:      vprintf replacement for Checker.      Copyright 1993, 1994, 1995 Tristan Gingold      Written September 1993 Tristan Gingold      Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE   (Checker itself was GPL'd.)   ------------------------------------------------------------------ */static HChar vex_toupper ( HChar c ){   if (c >= 'a' && c <= 'z')      return toHChar(c + ('A' - 'a'));   else      return c;}static Int vex_strlen ( const HChar* str ){   Int i = 0;   while (str[i] != 0) i++;   return i;}Bool vex_streq ( const HChar* s1, const HChar* s2 ){   while (True) {      if (*s1 == 0 && *s2 == 0)         return True;      if (*s1 != *s2)         return False;      s1++;      s2++;   }}/* Some flags.  */#define VG_MSG_SIGNED    1 /* The value is signed. */#define VG_MSG_ZJUSTIFY  2 /* Must justify with '0'. */#define VG_MSG_LJUSTIFY  4 /* Must justify on the left. */#define VG_MSG_PAREN     8 /* Parenthesize if present (for %y) */#define VG_MSG_COMMA    16 /* Add commas to numbers (for %d, %u) *//* Copy a string into the buffer. */static UIntmyvprintf_str ( void(*send)(HChar), Int flags, Int width, HChar* str,                 Bool capitalise ){#  define MAYBE_TOUPPER(ch) toHChar(capitalise ? vex_toupper(ch) : (ch))   UInt ret = 0;   Int i, extra;   Int len = vex_strlen(str);   if (width == 0) {      ret += len;      for (i = 0; i < len; i++)         send(MAYBE_TOUPPER(str[i]));      return ret;   }   if (len > width) {      ret += width;      for (i = 0; i < width; i++)         send(MAYBE_TOUPPER(str[i]));      return ret;   }   extra = width - len;   if (flags & VG_MSG_LJUSTIFY) {      ret += extra;      for (i = 0; i < extra; i++)         send(' ');   }   ret += len;   for (i = 0; i < len; i++)      send(MAYBE_TOUPPER(str[i]));   if (!(flags & VG_MSG_LJUSTIFY)) {      ret += extra;      for (i = 0; i < extra; i++)         send(' ');   }#  undef MAYBE_TOUPPER   return ret;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -