test3.c

00001 /* $Id: test2.c,v 1.1 2005/01/25 14:25:52 jlaako Exp $ */
00002 
00003 /*
00004 
00005     Shared memory object for POSIX/IEEE-1003.1 compliant systems.
00006 
00007     Copyright (C) 2005-2006 Nokia Corporation.
00008 
00009     Contact: Jussi Laako <jussi.laako@nokia.com>
00010 
00011     This library is free software; you can redistribute it and/or
00012     modify it under the terms of the GNU Lesser General Public
00013     License as published by the Free Software Foundation; either
00014     version 2.1 of the License, or (at your option) any later version.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public
00022     License along with this library; if not, write to the Free Software
00023     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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     /* create shared memory */
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     /* initialize allocation */
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     /* allocate four blocks */
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     /* reallocate two center blocks with different size */
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     /* print some info */
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     /* free the allocations  (in random order) */
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 }

Generated on Thu Sep 13 18:14:21 2007 for libsharedmem by  doxygen 1.5.1