-#if defined __linux__ /* || defined __CYGWIN__ */
-
- struct rofile rof;
- int c;
- unsigned long total;
-
- /* Open the current process' maps file. It describes one VMA per line. */
- if (rof_open (&rof, "/proc/self/maps") < 0)
- return 0;
-
- total = 0;
- for (;;)
- {
- unsigned long start, end;
-
- if (!(rof_scanf_lx (&rof, &start) >= 0
- && rof_getchar (&rof) == '-'
- && rof_scanf_lx (&rof, &end) >= 0))
- break;
- while (c = rof_getchar (&rof), c != -1 && c != '\n')
- ;
- total += end - start;
- }
- rof_close (&rof);
- return total;
-
-#elif defined __FreeBSD__
-
- struct rofile rof;
- int c;
- unsigned long total;
-
- /* Open the current process' maps file. It describes one VMA per line. */
- if (rof_open (&rof, "/proc/curproc/map") < 0)
- return 0;
-
- total = 0;
- for (;;)
- {
- unsigned long start, end;
-
- if (!(rof_getchar (&rof) == '0'
- && rof_getchar (&rof) == 'x'
- && rof_scanf_lx (&rof, &start) >= 0))
- break;
- while (c = rof_peekchar (&rof), c == ' ' || c == '\t')
- rof_getchar (&rof);
- if (!(rof_getchar (&rof) == '0'
- && rof_getchar (&rof) == 'x'
- && rof_scanf_lx (&rof, &end) >= 0))
- break;
- while (c = rof_getchar (&rof), c != -1 && c != '\n')
- continue;
- total += end - start;
- }
- rof_close (&rof);
- return total;
-
-#elif defined __sgi || defined __osf__ /* IRIX, OSF/1 */
-
- size_t pagesize;
- char fnamebuf[6+10+1];
- char *fname;
- int fd;
- int nmaps;
- size_t memneed;
-# if HAVE_MAP_ANONYMOUS
-# define zero_fd -1
-# define map_flags MAP_ANONYMOUS
-# else
- int zero_fd;
-# define map_flags 0
-# endif
- void *auxmap;
- unsigned long auxmap_start;
- unsigned long auxmap_end;
- prmap_t* maps;
- prmap_t* mp;
- unsigned long total;
-
- pagesize = getpagesize ();
-
- /* Construct fname = sprintf (fnamebuf+i, "/proc/%u", getpid ()). */
- fname = fnamebuf + sizeof (fnamebuf) - 1;
- *fname = '\0';
- {
- unsigned int value = getpid ();
- do
- *--fname = (value % 10) + '0';
- while ((value = value / 10) > 0);
- }
- fname -= 6;
- memcpy (fname, "/proc/", 6);
-
- fd = open (fname, O_RDONLY);
- if (fd < 0)
- return 0;
-
- if (ioctl (fd, PIOCNMAP, &nmaps) < 0)
- goto fail2;
-
- memneed = (nmaps + 10) * sizeof (prmap_t);
- /* Allocate memneed bytes of memory.
- We cannot use alloca here, because not much stack space is guaranteed.
- We also cannot use malloc here, because a malloc() call may call mmap()
- and thus pre-allocate available memory.
- So use mmap(), and ignore the resulting VMA. */
- memneed = ((memneed - 1) / pagesize + 1) * pagesize;
-# if !HAVE_MAP_ANONYMOUS
- zero_fd = open ("/dev/zero", O_RDONLY, 0644);
- if (zero_fd < 0)
- goto fail2;
-# endif
- auxmap = (void *) mmap ((void *) 0, memneed, PROT_READ | PROT_WRITE,
- map_flags | MAP_PRIVATE, zero_fd, 0);
-# if !HAVE_MAP_ANONYMOUS
- close (zero_fd);
-# endif
- if (auxmap == (void *) -1)
- goto fail2;
- auxmap_start = (unsigned long) auxmap;
- auxmap_end = auxmap_start + memneed;
- maps = (prmap_t *) auxmap;
-
- if (ioctl (fd, PIOCMAP, maps) < 0)
- goto fail1;
-
- total = 0;
- for (mp = maps;;)
- {
- unsigned long start, end;
-
- start = (unsigned long) mp->pr_vaddr;
- end = start + mp->pr_size;
- if (start == 0 && end == 0)
- break;
- mp++;
- if (start <= auxmap_start && auxmap_end - 1 <= end - 1)
- /* Consider [start,end-1] \ [auxmap_start,auxmap_end-1]
- = [start,auxmap_start-1] u [auxmap_end,end-1]. */
- total += (end - start) - memneed;
- else
- total += end - start;
- }
- munmap (auxmap, memneed);
- close (fd);
- return total;
-
- fail1:
- munmap (auxmap, memneed);
- fail2:
- close (fd);
- return 0;
-
-#elif defined __APPLE__ && defined __MACH__ /* MacOS X */
-
- task_t task = mach_task_self ();
- vm_address_t address;
- vm_size_t size;
- vm_address_t total = 0;