aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-03-15 00:57:01 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-03-15 00:57:01 +0000
commitf10971e4997d50bd72bdcf14154ba4007d1b65a1 (patch)
tree4d11a46c5e62a35d357fce9659577cecec8ea117 /docs
parentc46212e58fa6ecb453fd7f5648e2692a6518585b (diff)
downloadbusybox-w32-f10971e4997d50bd72bdcf14154ba4007d1b65a1.tar.gz
busybox-w32-f10971e4997d50bd72bdcf14154ba4007d1b65a1.tar.bz2
busybox-w32-f10971e4997d50bd72bdcf14154ba4007d1b65a1.zip
modify ptr_to_globals trick so that we do not violate
type safety (well, sort of ;)) git-svn-id: svn://busybox.net/trunk/busybox@18116 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'docs')
-rw-r--r--docs/keep_data_small.txt17
1 files changed, 10 insertions, 7 deletions
diff --git a/docs/keep_data_small.txt b/docs/keep_data_small.txt
index 272aa21c5..ec13b4d3f 100644
--- a/docs/keep_data_small.txt
+++ b/docs/keep_data_small.txt
@@ -59,30 +59,33 @@ archival/libunarchive/decompress_unzip.c:
59 59
60This example completely eliminates globals in that module. 60This example completely eliminates globals in that module.
61Required memory is allocated in inflate_gunzip() [its main module] 61Required memory is allocated in inflate_gunzip() [its main module]
62and then passed down to all subroutines which need to access globals 62and then passed down to all subroutines which need to access 'globals'
63as a parameter. 63as a parameter.
64 64
65 Example 2 65 Example 2
66 66
67In case you don't want to pass this additional parameter everywhere, 67In case you don't want to pass this additional parameter everywhere,
68take a look at archival/gzip.c. Here all global data is replaced by 68take a look at archival/gzip.c. Here all global data is replaced by
69singe global pointer (ptr_to_globals) to allocated storage. 69single global pointer (ptr_to_globals) to allocated storage.
70 70
71In order to not duplicate ptr_to_globals in every applet, you can 71In order to not duplicate ptr_to_globals in every applet, you can
72reuse single common one. It is defined in libbb/messages.c 72reuse single common one. It is defined in libbb/messages.c
73as void *ptr_to_globals, but is NOT declared in libbb.h. 73as struct globals *ptr_to_globals, but the struct globals is
74You first define a struct: 74NOT defined in libbb.h. You first define your own struct:
75 75
76struct my_globals { int a; char buf[1000]; }; 76struct globals { int a; char buf[1000]; };
77 77
78and then declare that ptr_to_globals is a pointer to it: 78and then declare that ptr_to_globals is a pointer to it:
79 79
80extern struct my_globals *ptr_to_globals;
81#define G (*ptr_to_globals) 80#define G (*ptr_to_globals)
82 81
83Linker magic enures that these two merge into single pointer object. 82Linker magic ensures that these two merge into single pointer object.
84Now initialize it in <applet>_main(): 83Now initialize it in <applet>_main():
85 84
86 ptr_to_globals = xzalloc(sizeof(G)); 85 ptr_to_globals = xzalloc(sizeof(G));
87 86
88and you can reference "globals" by G.a, G.buf and so on, in any function. 87and you can reference "globals" by G.a, G.buf and so on, in any function.
88
89The drawback is that now you have to initialize it by hand. xzalloc()
90can be helpful in clearing allocated storage to 0, but anything more
91must be done by hand.