00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include <sys/wait.h>
00032 #include <pthread.h>
00033
00034 #include "sharedmem.h"
00035 #include "shmalloc.h"
00036
00037
00038 int main (int argc, char *argv[])
00039 {
00040 long a1, a2, a3, a4;
00041 sharedmem_t shm;
00042 sharedmem_alloc_t shmalloc;
00043
00044
00045 if (sharedmem_create(&shm, "/test2.shm", 1048576) != 0)
00046 {
00047 printf("sharedmem_create(): %s\n", sharedmem_get_error_string(&shm));
00048 return 1;
00049 }
00050
00051
00052 if (sharedmem_alloc_create(&shmalloc, &shm) != 0)
00053 {
00054 printf("sharedmem_alloc_create(): %s\n",
00055 sharedmem_get_error_string(&shm));
00056 return 1;
00057 }
00058
00059
00060 a1 = sharedmem_alloc(&shmalloc, 256);
00061 if (a1 < 0)
00062 {
00063 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00064 goto bail_out;
00065 }
00066 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a1), "First");
00067
00068 a2 = sharedmem_alloc(&shmalloc, 256);
00069 if (a2 < 0)
00070 {
00071 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00072 goto bail_out;
00073 }
00074 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a2), "Second");
00075
00076 a3 = sharedmem_alloc(&shmalloc, 256);
00077 if (a3 < 0)
00078 {
00079 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00080 goto bail_out;
00081 }
00082 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a3), "Third");
00083
00084 a4 = sharedmem_alloc(&shmalloc, 256);
00085 if (a4 < 0)
00086 {
00087 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00088 goto bail_out;
00089 }
00090 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a4), "Fourth");
00091
00092 sharedmem_alloc_list_allocs(&shmalloc);
00093 sharedmem_alloc_list_frees(&shmalloc);
00094
00095
00096 a2 = sharedmem_realloc(&shmalloc, a2, 32);
00097 if (a2 < 0)
00098 {
00099 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00100 goto bail_out;
00101 }
00102 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a2), "Second");
00103
00104 a3 = sharedmem_realloc(&shmalloc, a3, 384);
00105 if (a3 < 0)
00106 {
00107 printf("sharedmem_alloc(): %s\n", sharedmem_get_error_string(&shm));
00108 goto bail_out;
00109 }
00110 strcpy(sharedmem_alloc_get_ptr(&shmalloc, a3), "Third");
00111
00112 sharedmem_alloc_list_allocs(&shmalloc);
00113 sharedmem_alloc_list_frees(&shmalloc);
00114
00115
00116 printf("%ld\t%ld\t%ld\t%ld\n", a1, a2, a3, a4);
00117 printf("%s\t%s\t%s\t%s\n",
00118 (char *) sharedmem_alloc_get_ptr(&shmalloc, a1),
00119 (char *) sharedmem_alloc_get_ptr(&shmalloc, a2),
00120 (char *) sharedmem_alloc_get_ptr(&shmalloc, a3),
00121 (char *) sharedmem_alloc_get_ptr(&shmalloc, a4));
00122
00123
00124 if (sharedmem_free(&shmalloc, a2) != 0)
00125 {
00126 printf("sharedmem_free(): %s\n", sharedmem_get_error_string(&shm));
00127 }
00128 if (sharedmem_free(&shmalloc, a4) != 0)
00129 {
00130 printf("sharedmem_free(): %s\n", sharedmem_get_error_string(&shm));
00131 }
00132 if (sharedmem_free(&shmalloc, a1) != 0)
00133 {
00134 printf("sharedmem_free(): %s\n", sharedmem_get_error_string(&shm));
00135 }
00136 if (sharedmem_free(&shmalloc, a3) != 0)
00137 {
00138 printf("sharedmem_free(): %s\n", sharedmem_get_error_string(&shm));
00139 }
00140
00141 puts("*** final ***");
00142
00143 sharedmem_alloc_list_allocs(&shmalloc);
00144 sharedmem_alloc_list_frees(&shmalloc);
00145
00146 bail_out:
00147 if (sharedmem_close(&shm) != 0)
00148 {
00149 printf("sharedmem_close(): %s\n", sharedmem_get_error_string(&shm));
00150 }
00151
00152 return 0;
00153 }