~ubuntu-branches/ubuntu/gutsy/funnelweb-doc/gutsy

« back to all changes in this revision

Viewing changes to developer/implement_memory.html

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-03-28 23:21:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020328232107-3d1jiqv1eqmco3j9
Tags: upstream-3.2d
ImportĀ upstreamĀ versionĀ 3.2d

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
 
2
            "http://www.w3.org/TR/REC-html40/loose.dtd">
 
3
 
 
4
<HTML>
 
5
<!--
 
6
***********************************************************************
 
7
FUNNELWEB MANUAL WEB PAGE
 
8
=========================
 
9
Copyright (c) Ross N. Williams 1992,1999. All rights reserved.
 
10
 
 
11
Permission is granted to redistribute and use this manual in
 
12
any medium, with or without modification, provided that all
 
13
notices (including, without limitation, the copyright
 
14
notice, this permission notice, any record of modification,
 
15
and all legal notices) are preserved on all copies, that all
 
16
modifications are clearly marked, and that modified versions
 
17
are not represented as the original version unless all the
 
18
modifications since the manual's original release by Ross N.
 
19
Williams (www.ross.net) consist of translations or other
 
20
transformations that alter only the manual's form, not its
 
21
content. THIS MANUAL IS PROVIDED "AS IS" AND WITHOUT ANY
 
22
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
 
23
LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
 
24
FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT PERMITTED BY
 
25
LAW THERE IS ABSOLUTELY NO WARRANTY.
 
26
 
 
27
***********************************************************************
 
28
-->
 
29
 
 
30
<HEAD>
 
31
<TITLE>3.4 Use of Memory</TITLE>
 
32
<STYLE TYPE="text/css"> <!-- A {text-decoration: none} // --> </STYLE>
 
33
</HEAD>
 
34
<BODY BACKGROUND="binary/background.gif"
 
35
      BGCOLOR="#FFFFFF"
 
36
      TEXT="#000000"
 
37
      VLINK="#660000"
 
38
      LINK="#FF0000"
 
39
      ALINK="#CC0000">
 
40
 
 
41
<TABLE WIDTH="490">
 
42
<TR>
 
43
<TD WIDTH="130" VALIGN="top">
 
44
<IMG SRC="binary/d_clear.gif" ALT="" WIDTH="130" HEIGHT="1"><BR>
 
45
 
 
46
<FONT SIZE="2">
 
47
<BR>
 
48
 
 
49
<A HREF="http://www.ross.net/"
 
50
 TARGET="rosshome"
 
51
 onClick="window.open('','rosshome','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
 
52
>
 
53
 <IMG SRC="binary/rossnet_logo.gif"
 
54
  WIDTH="64" HEIGHT="32"
 
55
  BORDER="0" ALT="RossNet"
 
56
  HSPACE="0" VSPACE="1"></A><BR>
 
57
<BR>
 
58
 
 
59
<A HREF="../index.shtml"
 
60
 TARGET="funnelweb"
 
61
 onClick="window.open('','funnelweb','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
 
62
>
 
63
 <IMG SRC="binary/linklogo.gif"
 
64
  WIDTH="64" HEIGHT="32"
 
65
  BORDER="0" ALT="FunnelWeb"
 
66
  HSPACE="0" VSPACE="1"></A><BR>
 
67
<BR>
 
68
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD BGCOLOR="#000000">
 
69
<A HREF="../reference/index.html"
 
70
 TARGET="funnelwebreference"
 
71
 onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
 
72
><FONT COLOR="#FFFFFF"><B>Reference</B></FONT></A><BR>
 
73
<BR>
 
74
<A HREF="../tutorial/index.html"
 
75
 TARGET="funnelwebtutorial"
 
76
 onClick="window.open('','funnelwebtutorial','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
 
77
><FONT COLOR="#FFFFFF"><B>Tutorial</B></FONT></A><BR>
 
78
<BR>
 
79
<A HREF="index.html"><FONT COLOR="#FFFFFF"><B>Developer</B></FONT></A><BR>
 
80
<A HREF="compile.html"><FONT COLOR="#FFFFFF">1 Compile</FONT></A><BR>
 
81
<A HREF="design.html"><FONT COLOR="#FFFFFF">2 Design</FONT></A><BR>
 
82
<A HREF="implement.html"><FONT COLOR="#FFFFFF">3 Implement</FONT></A><BR>
 
83
<A HREF="modify.html"><FONT COLOR="#FFFFFF">4 Modify</FONT></A><BR>
 
84
<A HREF="misc.html"><FONT COLOR="#FFFFFF">5 Misc</FONT></A><BR>
 
85
<A HREF="gpl.html"><FONT COLOR="#FFFFFF">6 Licence</FONT></A><BR>
 
86
 
 
87
<BR>
 
88
<A HREF="search.html"><FONT COLOR="#FFFFFF"><B>SEARCH</B></FONT></A><BR>
 
89
</FONT>
 
90
</TD></TR></TABLE>
 
91
 
 
92
 
 
93
</TD>
 
94
<TD WIDTH="360" VALIGN="top">
 
95
<FONT SIZE="3">
 
96
 
 
97
 
 
98
<A HREF="index.html"><IMG SRC="binary/title.gif"
 
99
 WIDTH="316" HEIGHT="24"
 
100
 BORDER="0" ALT="FunnelWeb Developer Manual"
 
101
 HSPACE="0" VSPACE="0"></A>
 
102
<P><FONT SIZE="5">3.4 Use of Memory</FONT><BR>
 
103
 
 
104
 
 
105
 
 
106
<P>FunnelWeb is not a memory-stressed program. However,
 
107
during its development, problems with the management of
 
108
memory seemed to crop up again and again. This section
 
109
documents some of these problems and the solutions
 
110
that were adopted.
 
111
 
 
112
<P>There are three places where memory can be obtained:
 
113
the heap, the stack, and from static variables. The
 
114
following three sections deal with each of these areas.
 
115
 
 
116
<P><BR><FONT SIZE="4"><STRONG>The Heap</STRONG></FONT><BR>
 
117
 
 
118
 
 
119
<P>One of the great frustrations of being a user is to
 
120
find that a computer program is complaining about lack of
 
121
memory when one knows full well that one has allocated at
 
122
least ten times as much memory to the program as it would
 
123
ever need to do its job. The reason for such error messages
 
124
usually has to do with the programmer setting a fixed
 
125
"reasonable" limit to a particular data structure
 
126
and then locking it up into an array whose bound is
 
127
specified by a constant. This malody is particularly common in
 
128
old Pascal programs. While the use of arrays can
 
129
increase the speed of a program, it also means that the user
 
130
cannot increase the capacity of the program without
 
131
obtaining the source code and recompiling it.
 
132
 
 
133
<P>The alternative is to use the heap for all data
 
134
structures that can grow in proportion to the size of the
 
135
user's input. This rule has been followed rigorously in
 
136
FunnelWeb. This means that as memory spaces increase, users
 
137
will be able to hand their version of FunnelWeb more memory
 
138
without having to recompile it.
 
139
 
 
140
<P><HR WIDTH=30%>
 
141
 
 
142
<P>Some problems arose early on the
 
143
Macintosh in the use of the heap.
 
144
For some obscure reason, many of the heap application
 
145
<SAMP>malloc</SAMP> calls were failing. Whatever
 
146
it was, it went away when I replaced direct calls to
 
147
<SAMP>malloc</SAMP> with calls to a mini package
 
148
I wrote (called <SAMP>memory</SAMP>) that allocated large
 
149
chunks of memory and then doled out small pieces as required
 
150
by the rest of the program.
 
151
 
 
152
<P>Having a package to manage all the memory allocation
 
153
had two other benefits.
 
154
 
 
155
<P>First, only one check was required in the entire
 
156
program to see if memory had run out (in the memory
 
157
package), and if that failed, the program could be brought
 
158
to a screaming halt. This organization was far preferable to
 
159
having each piece of code that needed to allocate memory
 
160
having to check to see if <SAMP>malloc</SAMP> had failed.
 
161
 
 
162
<P>Second, the decision to construct a mini-shell within
 
163
FunnelWeb to support regression testing meant that FunnelWeb
 
164
proper (the FunnelWeb <SAMP>fw</SAMP> shell command)
 
165
could be run many times in any given invocation of
 
166
FunnelWeb. As a consequence it was necessary to make sure
 
167
that there was no memory leakage
 
168
between invocations of FunnelWeb proper. This was
 
169
accomplished by reworking the memory package to operate a
 
170
watermark system. The user of the package, when requesting
 
171
memory, could request "temporary" or
 
172
"permanent". If permanent, the memory package forgot
 
173
that it had allocated the memory. If temporary, the memory
 
174
package places the allocated block on a list. There was then
 
175
a function in the memory package that could be called to
 
176
deallocate all the temporary memory. All this meant that
 
177
so long as all
 
178
requests for memory within FunnelWeb proper were for
 
179
temporary memory, and that memory was freed at the end of
 
180
every run, one could be sure that there was no memory
 
181
leakage.
 
182
 
 
183
<P><BR><FONT SIZE="4"><STRONG>The Stack</STRONG></FONT><BR>
 
184
 
 
185
 
 
186
<P>For a while during the development of FunnelWeb a
 
187
particularly nasty bug proved extremely hard to find. The
 
188
symptom was that FunnelWeb would crash, sometimes at random,
 
189
but more often upon entering a particular function. In the
 
190
end, about a day of specific debugging was required before
 
191
the problem was tracked down to a stack problem. It turned
 
192
out that somehow (either the fault of the Macintosh or the
 
193
THINK&nbsp;C language system), the compiler was allocating
 
194
just 6K for stack space!!!!!!!
 
195
 
 
196
<P>This experience led me immediately to go through the
 
197
entire program and eliminate (or remove to the heap) any
 
198
automatic variable declarations that used more than one
 
199
hundred or so bytes.
 
200
 
 
201
<P>The lesson is clearly that C&nbsp;programs that use more
 
202
than a few thousand bytes of stack space are risking their
 
203
portability. All large data structures should be placed in
 
204
the heap.
 
205
 
 
206
<P><BR><FONT SIZE="4"><STRONG>Static Variables</STRONG></FONT><BR>
 
207
 
 
208
 
 
209
<P>Static variables also proved a problem on the
 
210
Macintosh. It turned out that the Macintosh
 
211
THINK&nbsp;C compiler did not allow
 
212
more than 32K of statics <I>in the entire program</I>&nbsp;. For
 
213
a while this restriction was a serious threat to the program
 
214
as it was discovered that constant strings were included in
 
215
this total! However, some searching revealed a compiler
 
216
option that removed the strings from the static category.
 
217
 
 
218
<P>Nevertheless, the 32K limit is rather severe. Again, it
 
219
seems that for portability reasons, C&nbsp;programs that use a
 
220
lot of static variables are risking their portability. As a
 
221
result, the FunnelWeb code avoids static variables where
 
222
possible in favour of the heap.
 
223
 
 
224
 
 
225
<P>
 
226
<TABLE WIDTH="100%">
 
227
<TR>
 
228
<TD ALIGN="left"   VALIGN="bottom"><A HREF="implement_style.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD>
 
229
<TD ALIGN="center" VALIGN="bottom"><A HREF="implement.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD>
 
230
<TD ALIGN="right"  VALIGN="bottom"><A HREF="implement_indentation.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD>
 
231
</TR>
 
232
</TABLE>
 
233
 
 
234
 
 
235
 
 
236
<P>
 
237
<HR>
 
238
<FONT SIZE="2">
 
239
<A HREF="mailto:webmaster@ross.net">Webmaster</A>&nbsp;&nbsp;&nbsp;
 
240
<A HREF="copyright.html">Copyright &copy; Ross N. Williams 1992,1999. All rights reserved.</A><BR>
 
241
</FONT>
 
242
 
 
243
</FONT>
 
244
</TD>
 
245
</TR>
 
246
</TABLE>
 
247
 
 
248
 
 
249
</BODY>
 
250
 
 
251
<!-- *********************************************************************** -->
 
252
<!--      End Of A FunnelWeb Manual Web Page (www.ross.net/funnelweb/)       -->
 
253
<!-- *********************************************************************** -->
 
254
 
 
255
</HTML>