OSDN Git Service

bitstream_open_debug, bitstream_close_debug. and start/end
authorYoshihiro Yamazaki <yoya@awm.jp>
Tue, 21 Feb 2012 14:56:28 +0000 (23:56 +0900)
committerYoshihiro Yamazaki <yoya@awm.jp>
Tue, 21 Feb 2012 14:56:28 +0000 (23:56 +0900)
src/bitstream.c
src/bitstream.h
src/swf_object.c

index 6447f96..25867f6 100644 (file)
 
 static void bitstream_clear(bitstream_t *bs);
 
+
+#ifdef BITSTREAM_DEBUG /* bitstream debug */
+
+#undef bitstream_open
+#undef bitstream_close
+
+#define BITSTREAM_DEBUG_TABLE_NUM 0x40000
+
+static struct bitstream_debug_ {
+    void *ptr;
+    char *filename;
+    int  linenum;
+} bitstream_debug_table[BITSTREAM_DEBUG_TABLE_NUM];
+
+static int bitstream_debug_stack = 0;
+
+void
+bitstream_debug_start(void) {
+    int i;
+    bitstream_debug_stack ++;
+    if (bitstream_debug_stack > 1) {
+        fprintf(stderr, "bitstream_debug_start: bitstream_debug_stack=%d\n", bitstream_debug_stack);
+        return ;
+    }
+    for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
+        bitstream_debug_table[i].ptr = NULL;
+    }
+    fprintf(stderr, "bitstream_debug_start: 0/n=0/%d\n", BITSTREAM_DEBUG_TABLE_NUM);
+}
+
+void
+bitstream_debug_end(void) {
+    int i, j = 0;
+    bitstream_debug_stack --;
+    if (bitstream_debug_stack > 0) {
+        fprintf(stderr, "bitstream_debug_end: bitstream_debug_stack=%d\n", bitstream_debug_stack);
+        return ;
+    }
+    for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
+        if (bitstream_debug_table[i].ptr) {
+            fprintf(stderr, "XXX (%d) ptr=%p (%s, %d)\n",
+                    i, bitstream_debug_table[i].ptr,
+                    bitstream_debug_table[i].filename,
+                    bitstream_debug_table[i].linenum);
+            j = i + 1;
+        }
+    }
+    fprintf(stderr, "bitstream_debug_end: j/n=%d/%d\n", j, BITSTREAM_DEBUG_TABLE_NUM);
+}
+
+bitstream_t *
+bitstream_open_debug(char *filename, int linenum) {
+    int i;
+    void *ptr;
+    ptr = bitstream_open();
+    for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
+        if (bitstream_debug_table[i].ptr == NULL) {
+            bitstream_debug_table[i].ptr = ptr;
+            bitstream_debug_table[i].filename = filename;
+            bitstream_debug_table[i].linenum = linenum;
+            break;
+        }
+    }
+    return ptr;
+}
+
+void
+bitstream_close_debug(bitstream_t * bs,  char *filename, int linenum) {
+    int i;
+    void *ptr = bs;
+//    fprintf(stderr, "free_debug: ptr=%p (%s,%d)\n", ptr, filename, linenum);
+    for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
+        if (bitstream_debug_table[i].ptr == ptr) {
+            bitstream_debug_table[i].ptr = NULL;
+            break;
+        }
+    }
+    if (i == BITSTREAM_DEBUG_TABLE_NUM) {
+        char *p;
+        fprintf(stderr, "free non-allocated memory: ptr=%p (%s,%d)\n", ptr,
+                filename, linenum);
+        bitstream_debug_end();
+        p = ptr;
+        p = 0;
+    }
+    bitstream_close(bs);
+}
+
+#endif // BITSTREAM_DEBUG
+
 bitstream_t *
 bitstream_open(void) {
     bitstream_t *bs = (bitstream_t *) calloc(sizeof(*bs), 1);
+
     bs->data = NULL;
     bs->data_len = 0;
     bs->data_alloc_len = 0;
index e911659..cd1e4d2 100644 (file)
@@ -8,7 +8,6 @@
  *                     (C) 2008/03/09- yoya@awm.jp
  */
 
-
 #ifndef BITOPERATION_OPTIMIZE
 #define BITOPERATION_OPTIMIZE 0
 #endif
@@ -91,4 +90,22 @@ extern void bitstream_printerror(bitstream_t *bs);
 extern void bitstream_hexdump(bitstream_t *bs, int length);
 extern void bitstream_print(bitstream_t *bs);
 
+
+#ifdef BITSTREAM_DEBUG /* bitstream debug */
+
+extern void bitstream_debug_start(void);
+extern void bitstream_debug_end(void);
+
+#define bitstream_open()  bitstream_open_debug(__FILE__, __LINE__)
+#define bitstream_close(bs)  bitstream_close_debug(bs, __FILE__, __LINE__)
+extern bitstream_t *bitstream_open_debug(char *filename, int linenum);
+extern void bitstream_close_debug(bitstream_t *bs, char *filename, int linenum);
+
+#else // BITSTREAM_DEBUG
+
+#define bitstream_debug_start()
+#define bitstream_debug_end()
+
+#endif // BITSTREAM_DEBUG
+
 #endif /* __BITSTREAM_H__ */
index 8873316..bd5a472 100644 (file)
@@ -35,6 +35,10 @@ swf_object_open(void) {
 #ifdef MALLOC_DEBUG
     malloc_debug_start(); /* DEBUG XXX */
 #endif // MALLOC_DEBUG
+#ifdef BITSTREAM_DEBUG /* bitstream debug */
+    bitstream_debug_start();
+#endif // BITSTREAM_DEBUG
+
     swf = (swf_object_t *) calloc(sizeof(*swf), 1);
     swf->tag_head = NULL;
     swf->tag_tail = NULL;
@@ -65,6 +69,9 @@ swf_object_close(swf_object_t *swf) {
 #ifdef MALLOC_DEBUG
     malloc_debug_end(); /* DEBUG XXX */
 #endif // MALLOC_DEBUG
+#ifdef BITSTREAM_DEBUG /* bitstream debug */
+    bitstream_debug_end();
+#endif // BITSTREAM_DEBUG
     return ;
 }