/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
/* Regression test for bug 2583 */
#include <ios.h>
#include <iostypes.h>
#include <iosiobuf.h>
#include <ioslibc.h>

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

#define ALLOC_SIZE 64
#define MAX_BUFS 128  /* number of bufs of size ALLOC_SIZE */
#define TEST_ALLOC_SIZE 58

IOSIobDebugInfo d;

static void 
alloctst()
{
  int i;
  IOSIobuf *p[MAX_BUFS];
  IOSIobuf *q;
  u32 size = ALLOC_SIZE;
  u32 num = MAX_BUFS;
  
  memset(p, 0, sizeof(p));
  for (i = 0; i < num; ++i ) {
      p[i]  = IOS_AllocIob(POOL_DEFAULT, size, d);
      if ( NULL == p[i]) {
          printf("allocating iobuf of size %u: FAILED\n", size);
      }
  }
  size = 58;
  q = IOS_AllocIob(POOL_DEFAULT, size, d);
  if (NULL == q) {
      printf("2583 FAILED\n", size);
  }
  else {
      printf("2583 PASSED\n");
      IOS_FreeIob(q);
 }
  for (i = 0; i < num; ++i ) {
      if (NULL != p[i])
          IOS_FreeIob(p[i]);
  }
}

void
main(u32 arg)
{
    if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
        printf("getting access to iobuf pool failed\n");
        goto done;
    }
    alloctst();
done:
    IOS_DestroyThread(0, NULL);
}
