MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndbd_malloc.cpp
1 /*
2  Copyright (C) 2005-2007 MySQL AB, 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 #include "ndbd_malloc.hpp"
21 #include <NdbMem.h>
22 
23 //#define TRACE_MALLOC
24 #ifdef TRACE_MALLOC
25 #include <stdio.h>
26 #endif
27 
28 extern void do_refresh_watch_dog(Uint32 place);
29 
30 void
31 ndbd_alloc_touch_mem(void *p, size_t sz, volatile Uint32 * watchCounter)
32 {
33  Uint32 tmp = 0;
34  if (watchCounter == 0)
35  watchCounter = &tmp;
36 
37  unsigned char * ptr = (unsigned char*)p;
38  while (sz >= 4096)
39  {
40  * ptr = 0;
41  ptr += 4096;
42  sz -= 4096;
43  * watchCounter = 9;
44  }
45 }
46 
47 #ifdef TRACE_MALLOC
48 static void xxx(size_t size, size_t *s_m, size_t *s_k, size_t *s_b)
49 {
50  *s_m = size/1024/1024;
51  *s_k = (size - *s_m*1024*1024)/1024;
52  *s_b = size - *s_m*1024*1024-*s_k*1024;
53 }
54 #endif
55 
56 static Uint64 g_allocated_memory;
57 void *ndbd_malloc(size_t size)
58 {
59  void *p = NdbMem_Allocate(size);
60  if (p)
61  {
62  g_allocated_memory += size;
63 
64  ndbd_alloc_touch_mem(p, size, 0);
65 
66 #ifdef TRACE_MALLOC
67  {
68  size_t s_m, s_k, s_b;
69  xxx(size, &s_m, &s_k, &s_b);
70  fprintf(stderr, "%p malloc(%um %uk %ub)", p, s_m, s_k, s_b);
71  xxx(g_allocated_memory, &s_m, &s_k, &s_b);
72  fprintf(stderr, "\t\ttotal(%um %uk %ub)\n", s_m, s_k, s_b);
73  }
74 #endif
75  }
76  return p;
77 }
78 
79 void ndbd_free(void *p, size_t size)
80 {
81  NdbMem_Free(p);
82  if (p)
83  {
84  g_allocated_memory -= size;
85 #ifdef TRACE_MALLOC
86  fprintf(stderr, "%p free(%d)\n", p, size);
87 #endif
88  }
89 }