Logo Search packages:      
Sourcecode: libgphoto2 version File versions  Download package

static int gp_get_free_memory ( GPContext context,
unsigned *  free 
) [static]

gp_get_free_memory: : a GPContext :

Reads the amount of free kB:

  • free memory + free swap for Linux XXX - free memory for BSD (need a way to find free swap)

Return value: a gphoto2 error code.

Definition at line 1922 of file gphoto2-filesys.c.

{
#ifdef HAVE_PROCMEMINFO

      char buf[1024], *head, *tail, *tmp;
      int n, fd = -1;
      *free=0;

      if ((fd = open ("/proc/meminfo", O_RDONLY)) == -1) {
            gp_context_error (context, _("Could not open '/proc/meminfo' "
                  "for reading ('%m'). Make sure the proc filesystem "
                  "is mounted."));
            return (GP_ERROR);
      }
      
      lseek (fd, 0L, SEEK_SET);
      if ((n = read (fd, buf, sizeof (buf) - 1)) < 0) {
            gp_context_error (context, _("An error occured while "
                  "reading '/proc/meminfo' ('%m')."));
            return (GP_ERROR);
      }
      buf[n] = '\0';
      n = 0;
      head = buf;
      do {
            tail = strchr (head, ':');
            if (!tail)
                  break;
            *tail = '\0';
            tmp = head;
            head = tail + 1;
            if (!strcmp (tmp, "MemFree")) {
                  *free += strtoul (head, NULL, 10);
                  n++;
            } else if (!strcmp (tmp, "SwapFree")) {
                  *free += strtoul (head, NULL, 10);
                  n++;
            }
            tail = strchr (head, '\n');
            if (!tail)
                  break;
            head = tail + 1;
      } while (n != 2);
      return (GP_OK);

#elif HAVE_SYSCTL && (__FreeBSD__ || __NetBSD__ || __OpenBSD__ || __APPLE__ )

      int mib[2] = { CTL_HW, HW_PHYSMEM };
      unsigned long value;
      size_t valuelen = sizeof(value);
      *free=0;
      if (sysctl(mib, 2 , &value, &valuelen, NULL, 0) == -1) {
            gp_context_error (context, _("sysctl call failed ('%m')."));
            return (GP_ERROR);
      }
      *free=value;
      return (GP_OK);

#elif (defined(sun) || defined(__sun__)) && defined(__svr4__)

      long freemem=0;
      long freeswap=0;
      int page_size;
      static struct anoninfo anon;

      *free = 0;

      page_size = getpagesize();
      if ( (freemem = sysconf(_SC_AVPHYS_PAGES)) == -1 )
        {
          gp_context_error (context, _("sysconf call failed ('%m')."));
          return (GP_ERROR);
        }

      freemem *= page_size;

      if (swapctl(SC_AINFO, &anon) == -1)
        {
          gp_context_error (context, _("swapctl call failed ('%m')."));
          return (GP_ERROR);
        }

      freeswap = anon.ani_max - anon.ani_resv;
      freeswap *= page_size;

      printf("Free Memory : %ld Swap %ld\n",freemem,freeswap);

      *free = freemem + freeswap;
      return (GP_OK);

#else

      /* No way to know the free memory on this system */
      *free = UINT_MAX;
      return (GP_OK);

#endif

}


Generated by  Doxygen 1.6.0   Back to index