ftp.nice.ch/pub/next/unix/admin/sysinfo.1.1.0.s.tar.gz#/memory.c

This is memory.c in view mode; [Download] [Up]

/*
 * Copyright (c) 1992 Michael A. Cooper.
 * This software may be freely distributed provided it is not sold for 
 * profit and the author is credited appropriately.
 */

#ifndef lint
static char *RCSid = "$Header: /src/common/usc/bin/sysinfo/RCS/memory.c,v 1.12 1992/11/24 04:05:57 mcooper Exp mcooper $";
#endif

/*
 * $Log: memory.c,v $
 * Revision 1.12  1992/11/24  04:05:57  mcooper
 * New/cleaner KVM/nlist interface.
 *
 * Revision 1.11  1992/11/23  23:17:33  mcooper
 * Make DivRndUp extern.
 *
 * Revision 1.10  1992/07/07  23:28:38  mcooper
 * Put wrapper macro around nlist stuff to fix NeXT/Mach declaration
 * problem.  Now works with NeXT 2.* cc compiler.
 *
 * Revision 1.9  1992/04/26  23:32:06  mcooper
 * Add Copyright notice
 *
 * Revision 1.8  1992/04/17  01:07:59  mcooper
 * More de-linting
 *
 * Revision 1.7  1992/04/16  02:25:39  mcooper
 * Bug fixes, de-linting, and other changes found with CodeCenter.
 *
 * Revision 1.6  1992/03/31  02:35:53  mcooper
 * Change "nl" to "PhysmemNL".
 *
 * Revision 1.5  1992/03/31  01:55:17  mcooper
 * Use new CheckNlist to check nlist success.
 *
 * Revision 1.4  1992/03/31  00:22:54  mcooper
 * Add ctob() comment.
 *
 * Revision 1.3  1992/03/31  00:15:09  mcooper
 * Add error check for nlist.n_type.
 *
 * Revision 1.2  1992/03/28  23:13:07  mcooper
 * Don't set memory if == 0.
 *
 * Revision 1.1  1992/03/22  00:20:10  mcooper
 * Initial revision
 *
 */


/*
 * Memory related functions.
 */

#include <stdio.h>
#include "system.h"
#include "defs.h"

/*
 * Divide and Round Up
 */
extern int DivRndUp(Num, Div)
    unsigned long 		Num;
    unsigned long 		Div;
{
    int 			i;

    i = Num / Div;

    return((Num % Div) ? i+1 : i);
}

#if	defined(HAVE_KVM) && defined(HAVE_NLIST)

#include <fcntl.h>
#include <nlist.h>

static char PhysmemSYM[] = 	"_physmem";

/*
 * Common method of determining amount of physical memory in a
 * BSD Unix machine.
 *
 * Get memory by reading the variable "physmem" from the kernel
 * and the system page size.
 */ 
extern char *GetMemoryFromPhysmem()
{
    struct nlist	       *nlptr;
    static char 		Buf[BUFSIZ];
    unsigned long 		NPages, Bytes;
    int		 		Amount = -1;
    kvm_t		       *kd;

    if (kd = KVMopen()) {
	if ((nlptr = KVMnlist(kd, PhysmemSYM, (struct nlist *)NULL)) == NULL)
	    return((char *) NULL);

	if (CheckNlist(nlptr))
	    return((char *) NULL);

	if (KVMread(kd, nlptr->n_value, (char *)&NPages, 
		    sizeof(NPages)) >= 0) {
	    /*
	     * Could use ctob() instead of "Page Size * Num Pages",
	     * but this is more portable.
	     */
	    Bytes = (long) (getpagesize() * NPages);
	    if (Debug)
		printf("Bytes = %d NPages = %d pagesize = %d\n", 
		       Bytes, NPages, getpagesize());
	    Amount = DivRndUp(Bytes, MBYTES);
	}
    }

    if (kd)
	KVMclose(kd);

    if (Amount > 0) {
	(void) sprintf(Buf, "%d MB", Amount);
	return(Buf);
    } else
	return((char *) NULL);
}
#endif	/* HAVE_KVM && HAVE_NLIST */

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.