2002-08-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mono / handles / scratch.c
1 #include <config.h>
2 #include <glib.h>
3 #include <unistd.h>
4
5 #include <mono/io-layer/io-layer.h>
6
7 /* We're digging into handle internals here... */
8 #include <mono/io-layer/handles-private.h>
9 #include <mono/io-layer/wapi-private.h>
10
11 #define HDRSIZE sizeof(struct _WapiScratchHeader)
12
13 static guchar *printable (guchar *data, guint32 datalen)
14 {
15         static guchar buf[32];
16         guint32 i;
17         
18         if(datalen>=32) {
19                 datalen=31;
20         }
21
22         for(i=0; i<datalen; i++) {
23                 if(g_ascii_isprint (data[i])) {
24                         buf[i]=data[i];
25                 } else {
26                         buf[i]='.';
27                 }
28         }
29         buf[i]='\0';
30
31         return(buf);
32 }
33
34 int main (int argc, char **argv)
35 {
36         guint32 idx=0;
37         struct _WapiScratchHeader *hdr;
38         gboolean success;
39         int shm_id;
40         
41         _wapi_shared_data=_wapi_shm_attach (FALSE, &success, &shm_id);
42         if(success==FALSE) {
43                 g_error ("Failed to attach shared memory! (tried shared memory ID 0x%x)", shm_id);
44                 exit (-1);
45         }
46
47         hdr=(struct _WapiScratchHeader *)&_wapi_shared_data->scratch_base[0];
48         if(hdr->flags==0 && hdr->length==0) {
49                 g_print ("Scratch space unused\n");
50                 exit (0);
51         }
52         
53         while(idx < _WAPI_SHM_SCRATCH_SIZE) {
54                 hdr=(struct _WapiScratchHeader *)&_wapi_shared_data->scratch_base[idx];
55                 if(hdr->flags & WAPI_SHM_SCRATCH_FREE) {
56                         g_print ("Free block at %6d (index %6d), length %6d\n",
57                                  idx, idx+HDRSIZE, hdr->length);
58                 } else {
59                         guchar *data=&_wapi_shared_data->scratch_base[idx+HDRSIZE];
60                         
61                         g_print ("Used block at %6d (index %6d), length %6d, [%s]\n",
62                                  idx, idx+HDRSIZE, hdr->length,
63                                  printable (data, hdr->length));
64                 }
65
66                 idx+=(hdr->length+HDRSIZE);
67         }
68         
69         exit (0);
70 }