New test.
[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         
40         _wapi_shared_data=g_new0 (struct _WapiHandleShared_list *, 1);
41         _wapi_shared_scratch=g_new0 (struct _WapiHandleScratch, 1);
42         
43         success=_wapi_shm_attach (&_wapi_shared_data[0], &_wapi_shared_scratch);
44         if(success==FALSE) {
45                 g_error ("Failed to attach shared memory!");
46                 exit (-1);
47         }
48
49         hdr=(struct _WapiScratchHeader *)&_wapi_shared_scratch->scratch_data;
50         if(hdr->flags==0 && hdr->length==0) {
51                 g_print ("Scratch space unused\n");
52                 exit (0);
53         }
54         
55         while(idx < _wapi_shared_scratch->data_len) {
56                 hdr=(struct _WapiScratchHeader *)&_wapi_shared_scratch->scratch_data[idx];
57                 if(hdr->flags & WAPI_SHM_SCRATCH_FREE) {
58                         g_print ("Free block at %6d (index %6d), length %6d\n",
59                                  idx, idx+HDRSIZE, hdr->length);
60                 } else {
61                         guchar *data=&_wapi_shared_scratch->scratch_data[idx+HDRSIZE];
62                         
63                         g_print ("Used block at %6d (index %6d), length %6d, [%s]\n",
64                                  idx, idx+HDRSIZE, hdr->length,
65                                  printable (data, hdr->length));
66                 }
67
68                 idx+=(hdr->length+HDRSIZE);
69         }
70         
71         exit (0);
72 }