[MPlayer-dev-eng] [PATCH] use mmap() to simplify menu.conf parsing

Jason Lunz lunz at falooley.org
Sat Mar 8 21:43:09 CET 2003


libmenu/menu.c copies all of menu.conf into a malloc() buffer for
parsing. It's simpler to just mmap() the file.

The patch below also removes a couple of unused variables in
libmenu/menu_param.c.

Jason


Index: libmenu/menu.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmenu/menu.c,v
retrieving revision 1.9
diff -u -p -r1.9 menu.c
--- libmenu/menu.c	9 Feb 2003 20:18:20 -0000	1.9
+++ libmenu/menu.c	8 Mar 2003 20:39:38 -0000
@@ -6,6 +6,8 @@
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
 
 #include "../libvo/osd.h"
 #include "../libvo/font_load.h"
@@ -110,49 +112,37 @@ static int menu_parse_config(char* buffe
       
 
 /// This will build the menu_defs list from the cfg file
-#define BUF_STEP 1024
-#define BUF_MIN 128
-#define BUF_MAX BUF_STEP*1024
 int menu_init(char* cfg_file) {
-  char* buffer = NULL;
-  int bl = BUF_STEP, br = 0;
-  int f, fd;
+  struct stat st;
+  char* mapping = NULL;
+  int fd;
+  int f = 0;
+  
 #ifndef HAVE_FREETYPE
   if(vo_font == NULL)
-    return 0;
+    goto err;
 #endif
   fd = open(cfg_file, O_RDONLY);
   if(fd < 0) {
     printf("Can't open menu config file: %s\n",cfg_file);
-    return 0;
+    goto err;
   }
-  buffer = malloc(bl);
-  while(1) {
-    int r;
-    if(bl - br < BUF_MIN) {
-      if(bl >= BUF_MAX) {
-	printf("Menu config file is too big (> %d KB)\n",BUF_MAX/1024);
-	close(fd);
-	free(buffer);
-	return 0;
-      }
-      bl += BUF_STEP;
-      buffer = realloc(buffer,bl);
-    }
-    r = read(fd,buffer+br,bl-br);
-    if(r == 0) break;
-    br += r;
+  if(0 != fstat(fd, &st)) {
+    perror("fstat");
+    goto err_close;
   }
-  if(!br) {
-    printf("Menu config file is empty\n");
-    return 0;
+  if(MAP_FAILED == (mapping = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))) {
+    perror("mmap");
+    goto err_close;
   }
-  buffer[br-1] = '\0';
-
+  f = menu_parse_config(mapping);
+  if(0 != munmap(mapping, st.st_size)) {
+    perror("mmap");
+    f = 0;
+  }
+err_close:
   close(fd);
-
-  f = menu_parse_config(buffer);
-  free(buffer);
+err:
   return f;
 }
 
Index: libmenu/menu_param.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmenu/menu_param.c,v
retrieving revision 1.2
diff -u -p -r1.2 menu_param.c
--- libmenu/menu_param.c	9 Feb 2003 20:18:20 -0000	1.2
+++ libmenu/menu_param.c	8 Mar 2003 20:39:39 -0000
@@ -53,7 +53,7 @@ static m_option_t cfg_fields[] = {
 extern m_config_t* mconfig;
 
 static int parse_args(menu_t* menu,char* args) {
-  char *element,*body, **attribs, *name, *ok, *cancel;
+  char *element,*body, **attribs, *name;
   list_entry_t* m = NULL;
   int r;
   m_option_t* opt;



More information about the MPlayer-dev-eng mailing list