Logo ROOT  
Reference Guide
mrealloc.c
Go to the documentation of this file.
1/* @(#)root/clib:$Id$ */
2/* Author: */
3
4/* Change the size of a block allocated by `mmalloc'.
5 Copyright 1990, 1991 Free Software Foundation
6 Written May 1989 by Mike Haertel.
7
8The GNU C Library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Library General Public License as
10published by the Free Software Foundation; either version 2 of the
11License, or (at your option) any later version.
12
13The GNU C Library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Library General Public License for more details.
17
18You should have received a copy of the GNU Library General Public
19License along with the GNU C Library; see the file COPYING.LIB. If
20not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23 The author may be reached (Email) at the address mike@ai.mit.edu,
24 or (US mail) as Mike Haertel c/o Free Software Foundation. */
25
26#include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
27
28#include "mmprivate.h"
29
30/* Resize the given region to the new size, returning a pointer
31 to the (possibly moved) region. This is optimized for speed;
32 some benchmarks seem to indicate that greater compactness is
33 achieved by unconditionally allocating and copying to a
34 new region. This module has incestuous knowledge of the
35 internals of both mfree and mmalloc. */
36
37PTR mrealloc(PTR md, PTR ptr, size_t size)
38{
39 struct mdesc *mdp;
40 PTR result;
41 int type;
42 size_t block, blocks, oldlimit;
43
44 if (size == 0)
45 {
46 mfree (md, ptr);
47 return (mmalloc (md, 0));
48 }
49 else if (ptr == NULL)
50 {
51 return (mmalloc (md, size));
52 }
53
54 mdp = MD_TO_MDP (md);
55
56 if (mdp -> mrealloc_hook != NULL)
57 {
58 return ((*mdp -> mrealloc_hook) (md, ptr, size));
59 }
60
61 block = BLOCK (ptr);
62
63 type = mdp -> heapinfo[block].busy.type;
64 switch (type)
65 {
66 case 0:
67 /* Maybe reallocate a large block to a small fragment. */
68 if (size <= BLOCKSIZE / 2)
69 {
70 result = mmalloc (md, size);
71 if (result != NULL)
72 {
73 memcpy (result, ptr, size);
74 mfree (md, ptr);
75 return (result);
76 }
77 }
78
79 /* The new size is a large allocation as well;
80 see if we can hold it in place. */
81 blocks = BLOCKIFY (size);
82 if (blocks < mdp -> heapinfo[block].busy.info.size)
83 {
84 /* The new size is smaller; return excess memory to the free list. */
85 mdp -> heapinfo[block + blocks].busy.type = 0;
86 mdp -> heapinfo[block + blocks].busy.info.size
87 = mdp -> heapinfo[block].busy.info.size - blocks;
88 mdp -> heapinfo[block].busy.info.size = blocks;
89 mfree (md, ADDRESS (block + blocks));
90 result = ptr;
91 }
92 else if (blocks == mdp -> heapinfo[block].busy.info.size)
93 {
94 /* No size change necessary. */
95 result = ptr;
96 }
97 else
98 {
99 /* Won't fit, so allocate a new region that will.
100 Free the old region first in case there is sufficient
101 adjacent free space to grow without moving. */
102 blocks = mdp -> heapinfo[block].busy.info.size;
103 /* Prevent free from actually returning memory to the system. */
104 oldlimit = mdp -> heaplimit;
105 mdp -> heaplimit = 0;
106 mfree (md, ptr);
107 mdp -> heaplimit = oldlimit;
108 result = mmalloc (md, size);
109 if (result == NULL)
110 {
111 mmalloc (md, blocks * BLOCKSIZE);
112 return (NULL);
113 }
114 if (ptr != result)
115 {
116 memmove (result, ptr, blocks * BLOCKSIZE);
117 }
118 }
119 break;
120
121 default:
122 /* Old size is a fragment; type is logarithm
123 to base two of the fragment size. */
124 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
125 {
126 /* The new size is the same kind of fragment. */
127 result = ptr;
128 }
129 else
130 {
131 /* The new size is different; allocate a new space,
132 and copy the lesser of the new size and the old. */
133 result = mmalloc (md, size);
134 if (result == NULL)
135 {
136 return (NULL);
137 }
138 memcpy (result, ptr, MIN (size, (size_t) 1 << type));
139 mfree (md, ptr);
140 }
141 break;
142 }
143
144 return (result);
145}
146
147/* When using this package, provide a version of malloc/realloc/free built
148 on top of it, so that if we use the default sbrk() region we will not
149 collide with another malloc package trying to do the same thing, if
150 the application contains any "hidden" calls to malloc/realloc/free (such
151 as inside a system library). */
152
153#ifndef NO_SBRK_MALLOC
154
155PTR
157 PTR ptr;
158 size_t size;
159{
160 return (mrealloc ((PTR) NULL, ptr, size));
161}
162
163#endif
164
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
#define NULL
Definition ZInflate.c:15
#define realloc
Definition civetweb.c:1577
void mfree(PTR md, PTR ptr)
Definition mfree.c:206
PTR mmalloc(PTR md, size_t size)
Definition mmalloc.c:125
#define PTR
Definition mmalloc.h:29
#define MD_TO_MDP(md)
Definition mmprivate.h:375
#define BLOCKIFY(SIZE)
Definition mmprivate.h:82
#define ADDRESS(B)
Definition mmprivate.h:108
#define BLOCK(A)
Definition mmprivate.h:106
#define MIN(A, B)
Definition mmprivate.h:65
#define BLOCKSIZE
Definition mmprivate.h:81
PTR mrealloc(PTR md, PTR ptr, size_t size)
Definition mrealloc.c:37
size_t heaplimit
Definition mmprivate.h:259
mmalloc_info * heapinfo
Definition mmprivate.h:265
struct mmalloc_info::@004153014072076254376050275252017254170221213323 busy
union mmalloc_info::@004153014072076254376050275252017254170221213323::@020105015044372160031031201102202345236234014163 info
size_t size
Definition mmprivate.h:128