diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-15 00:57:01 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-15 00:57:01 +0000 |
commit | f10971e4997d50bd72bdcf14154ba4007d1b65a1 (patch) | |
tree | 4d11a46c5e62a35d357fce9659577cecec8ea117 /docs | |
parent | c46212e58fa6ecb453fd7f5648e2692a6518585b (diff) | |
download | busybox-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.txt | 17 |
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 | ||
60 | This example completely eliminates globals in that module. | 60 | This example completely eliminates globals in that module. |
61 | Required memory is allocated in inflate_gunzip() [its main module] | 61 | Required memory is allocated in inflate_gunzip() [its main module] |
62 | and then passed down to all subroutines which need to access globals | 62 | and then passed down to all subroutines which need to access 'globals' |
63 | as a parameter. | 63 | as a parameter. |
64 | 64 | ||
65 | Example 2 | 65 | Example 2 |
66 | 66 | ||
67 | In case you don't want to pass this additional parameter everywhere, | 67 | In case you don't want to pass this additional parameter everywhere, |
68 | take a look at archival/gzip.c. Here all global data is replaced by | 68 | take a look at archival/gzip.c. Here all global data is replaced by |
69 | singe global pointer (ptr_to_globals) to allocated storage. | 69 | single global pointer (ptr_to_globals) to allocated storage. |
70 | 70 | ||
71 | In order to not duplicate ptr_to_globals in every applet, you can | 71 | In order to not duplicate ptr_to_globals in every applet, you can |
72 | reuse single common one. It is defined in libbb/messages.c | 72 | reuse single common one. It is defined in libbb/messages.c |
73 | as void *ptr_to_globals, but is NOT declared in libbb.h. | 73 | as struct globals *ptr_to_globals, but the struct globals is |
74 | You first define a struct: | 74 | NOT defined in libbb.h. You first define your own struct: |
75 | 75 | ||
76 | struct my_globals { int a; char buf[1000]; }; | 76 | struct globals { int a; char buf[1000]; }; |
77 | 77 | ||
78 | and then declare that ptr_to_globals is a pointer to it: | 78 | and then declare that ptr_to_globals is a pointer to it: |
79 | 79 | ||
80 | extern struct my_globals *ptr_to_globals; | ||
81 | #define G (*ptr_to_globals) | 80 | #define G (*ptr_to_globals) |
82 | 81 | ||
83 | Linker magic enures that these two merge into single pointer object. | 82 | Linker magic ensures that these two merge into single pointer object. |
84 | Now initialize it in <applet>_main(): | 83 | Now initialize it in <applet>_main(): |
85 | 84 | ||
86 | ptr_to_globals = xzalloc(sizeof(G)); | 85 | ptr_to_globals = xzalloc(sizeof(G)); |
87 | 86 | ||
88 | and you can reference "globals" by G.a, G.buf and so on, in any function. | 87 | and you can reference "globals" by G.a, G.buf and so on, in any function. |
88 | |||
89 | The drawback is that now you have to initialize it by hand. xzalloc() | ||
90 | can be helpful in clearing allocated storage to 0, but anything more | ||
91 | must be done by hand. | ||