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 | |
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
-rw-r--r-- | archival/gzip.c | 9 | ||||
-rw-r--r-- | docs/keep_data_small.txt | 17 | ||||
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/messages.c | 3 |
4 files changed, 20 insertions, 13 deletions
diff --git a/archival/gzip.c b/archival/gzip.c index 9411e17a9..68f1cc87d 100644 --- a/archival/gzip.c +++ b/archival/gzip.c | |||
@@ -235,7 +235,7 @@ enum { | |||
235 | }; | 235 | }; |
236 | 236 | ||
237 | 237 | ||
238 | struct global1 { | 238 | struct globals { |
239 | 239 | ||
240 | lng block_start; | 240 | lng block_start; |
241 | 241 | ||
@@ -343,7 +343,6 @@ struct global1 { | |||
343 | uint32_t crc; /* shift register contents */ | 343 | uint32_t crc; /* shift register contents */ |
344 | }; | 344 | }; |
345 | 345 | ||
346 | extern struct global1 *ptr_to_globals; | ||
347 | #define G1 (*(ptr_to_globals - 1)) | 346 | #define G1 (*(ptr_to_globals - 1)) |
348 | 347 | ||
349 | 348 | ||
@@ -869,7 +868,7 @@ typedef struct tree_desc { | |||
869 | int max_code; /* largest code with non zero frequency */ | 868 | int max_code; /* largest code with non zero frequency */ |
870 | } tree_desc; | 869 | } tree_desc; |
871 | 870 | ||
872 | struct global2 { | 871 | struct globals2 { |
873 | 872 | ||
874 | ush heap[HEAP_SIZE]; /* heap used to build the Huffman trees */ | 873 | ush heap[HEAP_SIZE]; /* heap used to build the Huffman trees */ |
875 | int heap_len; /* number of elements in the heap */ | 874 | int heap_len; /* number of elements in the heap */ |
@@ -956,7 +955,7 @@ struct global2 { | |||
956 | ulg compressed_len; /* total bit length of compressed file */ | 955 | ulg compressed_len; /* total bit length of compressed file */ |
957 | }; | 956 | }; |
958 | 957 | ||
959 | #define G2ptr ((struct global2*)(ptr_to_globals)) | 958 | #define G2ptr ((struct globals2*)(ptr_to_globals)) |
960 | #define G2 (*G2ptr) | 959 | #define G2 (*G2ptr) |
961 | 960 | ||
962 | 961 | ||
@@ -2048,7 +2047,7 @@ int gzip_main(int argc, char **argv) | |||
2048 | } | 2047 | } |
2049 | #endif | 2048 | #endif |
2050 | 2049 | ||
2051 | ptr_to_globals = xzalloc(sizeof(struct global1) + sizeof(struct global2)); | 2050 | ptr_to_globals = xzalloc(sizeof(struct globals) + sizeof(struct globals2)); |
2052 | ptr_to_globals++; | 2051 | ptr_to_globals++; |
2053 | G2.l_desc.dyn_tree = G2.dyn_ltree; | 2052 | G2.l_desc.dyn_tree = G2.dyn_ltree; |
2054 | G2.l_desc.static_tree = G2.static_ltree; | 2053 | G2.l_desc.static_tree = G2.static_ltree; |
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. | ||
diff --git a/include/libbb.h b/include/libbb.h index 9ea9f5ae6..9a6966dbd 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -806,6 +806,10 @@ extern const int const_int_1; | |||
806 | #define BUFSIZ 4096 | 806 | #define BUFSIZ 4096 |
807 | #endif | 807 | #endif |
808 | extern char bb_common_bufsiz1[BUFSIZ+1]; | 808 | extern char bb_common_bufsiz1[BUFSIZ+1]; |
809 | /* This struct is deliberately not defined. */ | ||
810 | /* See docs/keep_data_small.txt */ | ||
811 | struct globals; | ||
812 | extern struct globals *ptr_to_globals; | ||
809 | 813 | ||
810 | /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it, | 814 | /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it, |
811 | * use bb_default_login_shell and following defines. | 815 | * use bb_default_login_shell and following defines. |
diff --git a/libbb/messages.c b/libbb/messages.c index 39bb4c95b..1a10a8c9e 100644 --- a/libbb/messages.c +++ b/libbb/messages.c | |||
@@ -56,4 +56,5 @@ WTMP_FILE; | |||
56 | 56 | ||
57 | char bb_common_bufsiz1[BUFSIZ+1]; | 57 | char bb_common_bufsiz1[BUFSIZ+1]; |
58 | 58 | ||
59 | void *ptr_to_globals; | 59 | struct globals; |
60 | struct globals *ptr_to_globals; | ||