shm: remove shared memory leak
authorgiwoong.kim <giwoong.kim@samsung.com>
Tue, 22 Jan 2013 10:31:21 +0000 (19:31 +0900)
committergiwoong.kim <giwoong.kim@samsung.com>
Tue, 22 Jan 2013 10:31:21 +0000 (19:31 +0900)
remove shared memory leak on Mac

Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/hw/maru_vga.c
tizen/src/maru_display.c
tizen/src/skin/client/src/org/tizen/emulator/skin/EmulatorShmSkin.java

index ebb996b..4f1c0e5 100644 (file)
 #endif
 
 #ifdef CONFIG_USE_SHM
-void *shared_memory = (void*)0;
+void *shared_memory = (void*) 0;
+int shmid;
 #endif
 
+
 MULTI_DEBUG_CHANNEL(qemu, maru_vga);
 
 extern pthread_mutex_t mutex_screenshot;
@@ -1488,13 +1490,15 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
     /* for screenshot */
     pthread_mutex_lock(&mutex_screenshot);
     MaruScreenshot* maru_screenshot = get_maru_screenshot();
-    if ( !maru_screenshot ) {
-        ERR( "maru screenshot is NULL.\n" );
+
+    if (!maru_screenshot) {
+        ERR("maru screenshot is NULL.\n");
     } else {
         if (maru_screenshot->request_screenshot == 1) {
             memcpy(maru_screenshot->pixel_data, s->ds->surface->data, 
                 s->ds->surface->linesize * s->ds->surface->height);
             maru_screenshot->request_screenshot = 0;
+
             pthread_cond_signal(&cond_screenshot);
         }
     }
@@ -1871,18 +1875,17 @@ void maru_vga_common_init(VGACommonState *s)
 #ifdef CONFIG_USE_SHM
     int mykey;
     void *temp;
-    int shmid;
 
     shmid = shmget((key_t)SHMKEY, (size_t)MAXLEN, 0666 | IPC_CREAT);
     if (shmid == -1) {
-        ERR( "shmget failed\n");
+        ERR("shmget failed\n");
         perror("maru_vga: ");
         exit(1);
     }
 
     temp = shmat(shmid, (char*)0x0, 0);
     if (temp == (void *)-1) {
-        ERR( "shmat failed\n");
+        ERR("shmat failed\n");
         perror("maru_vga: ");
         exit(1);
     }
@@ -1890,9 +1893,10 @@ void maru_vga_common_init(VGACommonState *s)
     mykey = atoi(temp);
     shmdt(temp);
     INFO("shared memory key: %d, vga ram_size : %d\n", mykey, s->vram_size);
+
     shmid = shmget((key_t)mykey, (size_t)s->vram_size, 0666 | IPC_CREAT);
     if (shmid == -1) {
-           ERR( "shmget failed\n");
+        ERR("shmget failed\n");
         perror("maru_vga: ");
         maru_register_exit_msg(MARU_EXIT_UNKNOWN, (char*)"Cannot launch this VM.\n"
                         "Shared memory is not enough.");
@@ -1901,7 +1905,7 @@ void maru_vga_common_init(VGACommonState *s)
 
     shared_memory = shmat(shmid, (void*)0, 0);
     if (shared_memory == (void *)-1) {
-        ERR( "shmat failed\n");
+        ERR("shmat failed\n");
         perror("maru_vga: ");
         exit(1);
     }
@@ -1912,6 +1916,20 @@ void maru_vga_common_init(VGACommonState *s)
 
 }
 
+#ifdef CONFIG_USE_SHM
+void maru_vga_common_fini(void)
+{
+    if (shmdt(shared_memory) == -1) {
+        ERR("shmdt failed\n");
+        perror("maru_vga: ");
+    }
+
+    if (shmctl(shmid, IPC_RMID, 0) == -1) {
+        ERR("shmctl failed\n");
+        perror("maru_vga: ");
+    }
+}
+#endif
 
 static const MemoryRegionPortio vga_portio_list[] = {
     { 0x04,  2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3b4 */
index f04f7ea..d76ad76 100644 (file)
@@ -73,11 +73,13 @@ void maru_display_init(DisplayState *ds)
 void maru_display_fini(void)
 {
     INFO("fini qemu display\n");
+
     g_free(maru_screenshot);
+
 #ifndef CONFIG_USE_SHM
     maruskin_sdl_quit();
 #else
-    //TODO:
+    maru_vga_common_fini();
 #endif
 }
 
index c75613a..a1dec15 100644 (file)
@@ -71,13 +71,11 @@ public class EmulatorShmSkin extends EmulatorSkin {
                private int[] array;
                private ImageData imageData;
                private Image framebuffer;
-               private EmulatorFingers finger;
 
                private volatile boolean stopRequest;
                private Runnable runnable;
 
                public PollFBThread(EmulatorFingers finger, int lcdWidth, int lcdHeight) {
-                       this.finger = finger;
                        this.display = Display.getDefault();
                        this.lcdWidth = lcdWidth;
                        this.lcdHeight = lcdHeight;
@@ -101,14 +99,15 @@ public class EmulatorShmSkin extends EmulatorSkin {
                        while (!stopRequest) {
                                synchronized (this) {
                                        try {
-                                               this.wait(30); //30ms
-                                       } catch (InterruptedException ex) {
+                                               this.wait(30); /* 30ms */
+                                       } catch (InterruptedException e) {
+                                               e.printStackTrace();
                                                break;
                                        }
                                }
 
-                               int result = getPixels(array); //from shared memory
-                               //logger.info("getPixels navtive function returned " + result);
+                               int result = getPixels(array); /* from shared memory */
+                               //logger.info("getPixels native function returned " + result);
 
                                for (int i = 0; i < lcdHeight; i++) {
                                        imageData.setPixels(0, i, lcdWidth, array, i * lcdWidth);
@@ -117,12 +116,17 @@ public class EmulatorShmSkin extends EmulatorSkin {
                                Image temp = framebuffer;
                                framebuffer = new Image(display, imageData);
                                temp.dispose();
+
                                if(display.isDisposed() == false) {
-                                       display.asyncExec(runnable); //redraw canvas
+                                       /* redraw canvas */
+                                       display.asyncExec(runnable);
                                }
                        }
 
-                       //logger.info("PollFBThread is stopped");
+                       logger.info("PollFBThread is stopped");
+
+                       int result = shmdt();
+                       logger.info("shmdt native function returned " + result);
                }
 
                public void stopRequest() {
@@ -156,7 +160,7 @@ public class EmulatorShmSkin extends EmulatorSkin {
                int result = shmget(
                                currentState.getCurrentResolutionWidth() *
                                currentState.getCurrentResolutionHeight());
-               //logger.info("shmget navtive function returned " + result);
+               logger.info("shmget native function returned " + result);
 
                /* update lcd thread */
                pollThread = new PollFBThread(finger,
@@ -229,8 +233,8 @@ public class EmulatorShmSkin extends EmulatorSkin {
                                finger.drawImage(e, currentState.getCurrentAngle());
                        }
                });
-        
-        pollThread.start();
+
+               pollThread.start();
 
                return 0;
        }