//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/7999585e-fdab-4e7b-b86c-67d55e862138/nks/Memory.c

//Warning: This code has been marked up for HTML

/***************************************************************************
$name: Memory.c 
$version: 1.0 
$date_modified: 101399 
$description: Demonstrates making memory allocation calls.
$owner: NKS Product Manager 
Copyright (c) 1999 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <nks/plat.h>
#include <nks/errno.h>
#include <nks/memory.h>

#define NODE_NAME "This is my name!"

typedef struct node
{
   struct node      *next;
   int64_t         fieldA[4];
   unsigned long  fieldB;
   char            *name;
   struct node      *link;
} Node;

int foo
(
   Node *copyNode
)
{
   int            err;
   unsigned int   cacheLineSize;
   char            *name;
   size_t         pageSize;
   void            *page;
   Node            *node;

   // sizes...

   pageSize      = NXGetPageSize();
   cacheLineSize = NXGetCacheLineSize();

   if (offsetof(Node, link) > cacheLineSize)
      printf("Hot 'link' field not at optimal offset on Pentium");

   // physical memory...

   name = (char *) NXMemAlloc(sizeof(NODE_NAME), 0);

   if (!name)
      return NX_ENOMEM;

   strcpy(name, NODE_NAME);

   // this string will have to be freed later using NXMemFree(node)...

   // . . .


   // virtual memory allocate a page and lock down Node's worth of it...

   page = NXPageAlloc(2, FALSE);

   if (!page)
      return NX_ENOMEM;

   err = NXMemCtl(page, sizeof(Node), NX_MEM_LOCK);

   node       = (Node *) page;
   node->name = name;

   NXPageFree(page);

   return 0;
}