/[qemu]/qemu/osdep.c
ViewVC logotype

Diff of /qemu/osdep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by bellard, Tue Aug 3 22:09:30 2004 UTC revision 1.7 by bellard, Thu Feb 10 21:59:15 2005 UTC
# Line 273  void *get_mmap_addr(unsigned long size) Line 273  void *get_mmap_addr(unsigned long size)
273    
274  #else  #else
275    
276    #include <malloc.h>
277    
278  int qemu_write(int fd, const void *buf, size_t n)  int qemu_write(int fd, const void *buf, size_t n)
279  {  {
280      int ret;      int ret;
# Line 298  void *qemu_malloc(size_t size) Line 300  void *qemu_malloc(size_t size)
300      return malloc(size);      return malloc(size);
301  }  }
302    
303    #if defined(USE_KQEMU)
304    
305    #include <sys/mman.h>
306    #include <fcntl.h>
307    
308    void *qemu_vmalloc(size_t size)
309    {
310        static int phys_ram_fd = -1;
311        static int phys_ram_size = 0;
312        const char *tmpdir;
313        char phys_ram_file[1024];
314        void *ptr;
315    
316        if (phys_ram_fd < 0) {
317            tmpdir = getenv("QEMU_TMPDIR");
318            if (!tmpdir)
319                tmpdir = "/dev/shm";
320            snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
321                     tmpdir);
322            if (mkstemp(phys_ram_file) < 0) {
323                fprintf(stderr,
324                        "warning: could not create temporary file in '%s'.\n"
325                        "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
326                        "Using '/tmp' as fallback.\n",
327                        tmpdir);
328                snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
329                         "/tmp");
330                if (mkstemp(phys_ram_file) < 0) {
331                    fprintf(stderr, "Could not create temporary memory file '%s'\n",
332                            phys_ram_file);
333                    exit(1);
334                }
335            }
336            phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
337            if (phys_ram_fd < 0) {
338                fprintf(stderr, "Could not open temporary memory file '%s'\n",
339                        phys_ram_file);
340                exit(1);
341            }
342            unlink(phys_ram_file);
343        }
344        size = (size + 4095) & ~4095;
345        ftruncate(phys_ram_fd, phys_ram_size + size);
346        ptr = mmap(NULL,
347                   size,
348                   PROT_WRITE | PROT_READ, MAP_SHARED,
349                   phys_ram_fd, phys_ram_size);
350        if (ptr == MAP_FAILED) {
351            fprintf(stderr, "Could not map physical memory\n");
352            exit(1);
353        }
354        phys_ram_size += size;
355        return ptr;
356    }
357    
358    void qemu_vfree(void *ptr)
359    {
360        /* may be useful some day, but currently we do not need to free */
361    }
362    
363    #else
364    
365    /* alloc shared memory pages */
366    void *qemu_vmalloc(size_t size)
367    {
368    #ifdef _BSD
369        return valloc(size);
370    #else
371        return memalign(4096, size);
372    #endif
373    }
374    
375    void qemu_vfree(void *ptr)
376    {
377        free(ptr);
378    }
379    
380    #endif
381    
382  #endif  #endif
383    
384  void *qemu_mallocz(size_t size)  void *qemu_mallocz(size_t size)

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26