svn commit: trunk/busybox: archival docs include libbb

vda at busybox.net vda at busybox.net
Thu Mar 15 00:57:02 UTC 2007


Author: vda
Date: 2007-03-14 17:57:01 -0700 (Wed, 14 Mar 2007)
New Revision: 18116

Log:
modify ptr_to_globals trick so that we do not violate
type safety (well, sort of ;))


Modified:
   trunk/busybox/archival/gzip.c
   trunk/busybox/docs/keep_data_small.txt
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/messages.c


Changeset:
Modified: trunk/busybox/archival/gzip.c
===================================================================
--- trunk/busybox/archival/gzip.c	2007-03-14 23:00:26 UTC (rev 18115)
+++ trunk/busybox/archival/gzip.c	2007-03-15 00:57:01 UTC (rev 18116)
@@ -235,7 +235,7 @@
 };
 
 
-struct global1 {
+struct globals {
 
 	lng block_start;
 
@@ -343,7 +343,6 @@
 	uint32_t crc;	/* shift register contents */
 };
 
-extern struct global1 *ptr_to_globals;
 #define G1 (*(ptr_to_globals - 1))
 
 
@@ -869,7 +868,7 @@
 	int max_code;		/* largest code with non zero frequency */
 } tree_desc;
 
-struct global2 {
+struct globals2 {
 
 	ush heap[HEAP_SIZE];     /* heap used to build the Huffman trees */
 	int heap_len;            /* number of elements in the heap */
@@ -956,7 +955,7 @@
 	ulg compressed_len;      /* total bit length of compressed file */
 };
 
-#define G2ptr ((struct global2*)(ptr_to_globals))
+#define G2ptr ((struct globals2*)(ptr_to_globals))
 #define G2 (*G2ptr)
 
 
@@ -2048,7 +2047,7 @@
 	}
 #endif
 
-	ptr_to_globals = xzalloc(sizeof(struct global1) + sizeof(struct global2));
+	ptr_to_globals = xzalloc(sizeof(struct globals) + sizeof(struct globals2));
 	ptr_to_globals++;
 	G2.l_desc.dyn_tree    = G2.dyn_ltree;
 	G2.l_desc.static_tree = G2.static_ltree;

Modified: trunk/busybox/docs/keep_data_small.txt
===================================================================
--- trunk/busybox/docs/keep_data_small.txt	2007-03-14 23:00:26 UTC (rev 18115)
+++ trunk/busybox/docs/keep_data_small.txt	2007-03-15 00:57:01 UTC (rev 18116)
@@ -59,30 +59,33 @@
 
 This example completely eliminates globals in that module.
 Required memory is allocated in inflate_gunzip() [its main module]
-and then passed down to all subroutines which need to access globals
+and then passed down to all subroutines which need to access 'globals'
 as a parameter.
 
 	Example 2
 
 In case you don't want to pass this additional parameter everywhere,
 take a look at archival/gzip.c. Here all global data is replaced by
-singe global pointer (ptr_to_globals) to allocated storage.
+single global pointer (ptr_to_globals) to allocated storage.
 
 In order to not duplicate ptr_to_globals in every applet, you can
 reuse single common one. It is defined in libbb/messages.c
-as void *ptr_to_globals, but is NOT declared in libbb.h.
-You first define a struct:
+as struct globals *ptr_to_globals, but the struct globals is
+NOT defined in libbb.h. You first define your own struct:
 
-struct my_globals { int a; char buf[1000]; };
+struct globals { int a; char buf[1000]; };
 
 and then declare that ptr_to_globals is a pointer to it:
 
-extern struct my_globals *ptr_to_globals;
 #define G (*ptr_to_globals)
 
-Linker magic enures that these two merge into single pointer object.
+Linker magic ensures that these two merge into single pointer object.
 Now initialize it in <applet>_main():
 
 	ptr_to_globals = xzalloc(sizeof(G));
 
 and you can reference "globals" by G.a, G.buf and so on, in any function.
+
+The drawback is that now you have to initialize it by hand. xzalloc()
+can be helpful in clearing allocated storage to 0, but anything more
+must be done by hand.

Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-03-14 23:00:26 UTC (rev 18115)
+++ trunk/busybox/include/libbb.h	2007-03-15 00:57:01 UTC (rev 18116)
@@ -806,6 +806,10 @@
 #define BUFSIZ 4096
 #endif
 extern char bb_common_bufsiz1[BUFSIZ+1];
+/* This struct is deliberately not defined. */
+/* See docs/keep_data_small.txt */
+struct globals;
+extern struct globals *ptr_to_globals;
 
 /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  * use bb_default_login_shell and following defines.

Modified: trunk/busybox/libbb/messages.c
===================================================================
--- trunk/busybox/libbb/messages.c	2007-03-14 23:00:26 UTC (rev 18115)
+++ trunk/busybox/libbb/messages.c	2007-03-15 00:57:01 UTC (rev 18116)
@@ -56,4 +56,5 @@
 
 char bb_common_bufsiz1[BUFSIZ+1];
 
-void *ptr_to_globals;
+struct globals;
+struct globals *ptr_to_globals;




More information about the busybox-cvs mailing list