Beispiel #1
0
__private_extern__ ANPacketInfo * ANControlListEntryGetPacket(OSMallocTag tag, ANControlListEntry * entry) {
    if (entry->bufferLength < 8) return NULL;
    uint32_t length = ((uint32_t *)entry->packetBuffer)[1];
    if (length > entry->bufferLength) return NULL;
    
    // allocate the packet and copy the buffer into it
    ANPacketInfo * info = (ANPacketInfo *)OSMalloc(length, tag);
    if (!info) return NULL;
    memcpy(info, entry->packetBuffer, length);
    
    // move the remaining data back in the buffer
    if (length == entry->bufferLength) {
        // we used up the entire buffer
        OSFree(entry->packetBuffer, entry->bufferLength, tag);
        entry->packetBuffer = NULL;
        entry->bufferLength = 0;
    } else {
        uint32_t newLength = entry->bufferLength - length;
        char * newBuffer = (char *)OSMalloc(newLength, tag);
        if (newBuffer) {
            memcpy(newBuffer, &entry->packetBuffer[length], newLength);            
            OSFree(entry->packetBuffer, entry->bufferLength, tag);
            entry->packetBuffer = newBuffer;
            entry->bufferLength = newLength;
        } else {
            OSFree(entry->packetBuffer, entry->bufferLength, tag);
            entry->packetBuffer = 0;
            entry->bufferLength = 0;
        }
    }
    return info;
}
Beispiel #2
0
/*! \brief Close and free a semaphore
 *
 * UNIX/Linux
 *
 * \param[in] semaHandle Pointer returned by \c PMS_CreateSemaphore.
*/
void PMS_DestroySemaphore(void * semaHandle)
{
#ifdef MACOSX
  int res;
  mac_sema_desc_t *macsem = (mac_sema_desc_t*)semaHandle;
  char name[SEM_NAME_LEN];

  sprintf(name, "pms-%d-%d", getpid(), macsem->semno);
  res = sem_unlink(name);
  res = sem_close(macsem->sem);
  OSFree(macsem,PMS_MemoryPoolPMS);
  sem_count--;
#else
  sem_t * sem = (sem_t*)semaHandle;

  while (sem_destroy(sem)) {
    if (sem_post(sem)) break;
  }
  /* No option to return an error - just free sem and hope. */
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  OSFree(sem,PMS_MemoryPoolPMS);
#else
  mfree(sem);
#endif
#endif
}
Beispiel #3
0
// Frees 'buffer', including the enclosed string pointer.
void pmem_free(pmem_OSBuffer *buffer) {
    if (!buffer) {
        return;
    }

    if (buffer->buffer) {
        OSFree(buffer->buffer, buffer->size, buffer->tag);
    }

    OSFree(buffer, sizeof(pmem_OSBuffer), buffer->tag);
}
Beispiel #4
0
// Tries to free all resources and also passes through any errors
//
// args: the error arg will be overwritten with KERN_FAILURE in case of an error
//       or returned unmodified in case everything went well.
// return: the given error argument or KERN_FAILURE if anything went wrong
static int pmem_cleanup(int error) {
  if (pmem_zero_page) {
    OSFree(pmem_zero_page, PAGE_SIZE, pmem_tag);
  }
  if (pte_mmap != NULL) {
    pte_mmap_osx_delete(pte_mmap);
  }
  if (pmem_tag) {
    OSMalloc_Tagfree(pmem_tag);
  }
  if (pmem_devpmemnode) {
    devfs_remove(pmem_devpmemnode);
  }
  if (pmem_devmajor != -1) {
    int devindex = 0;
    devindex = cdevsw_remove(pmem_devmajor, &pmem_cdevsw);
    if (devindex != pmem_devmajor) {
      pmem_error("Failed to remove cdevsw, cdevsw_remove() returned %d,"
                 "should be %d", devindex, pmem_devmajor);
      pmem_error("Kext will not be unloaded as an uio could result"
                 " in calling non-existent code");
      error = KERN_FAILURE;
    }
  }

  return error;
}
Beispiel #5
0
static void
ipsec_free(void *ptr)
{
	size_t	*size = ptr;
	size--;
	OSFree(size, *size, ipsec_malloc_tag);
}
Beispiel #6
0
// Allocates a pmem_OSBuffer instance of 'size' and 'tag'.
// Returns 0 on failure.
pmem_OSBuffer *pmem_alloc(uint32_t size, OSMallocTag_t tag) {
    pmem_OSBuffer *buffer = (pmem_OSBuffer *)OSMalloc(sizeof(pmem_OSBuffer),
                                                      tag);
    if (!buffer) {
        goto error;
    }

    bzero(buffer, sizeof(pmem_OSBuffer));
    buffer->size = size;
    buffer->tag = tag;
    buffer->buffer = (char *)OSMalloc(size, tag);

    if (!buffer->buffer) {
        goto error;
    }

    bzero(buffer->buffer, buffer->size);

    return buffer;

error:
    if (buffer) {
        OSFree(buffer, sizeof(pmem_OSBuffer), tag);
    }

    return 0;
}
Beispiel #7
0
void
utun_free(void *ptr)
{
	size_t	*size = ptr;
	size--;
	OSFree(size, *size, utun_malloc_tag);
}
Beispiel #8
0
/*! \brief Create a semaphore
 *
 * UNIX/Linux
 *
 * Semaphore is casted to \c void* for platform portability.
 *
 * \param[in] initialValue Initial semaphore value.
 * \return Pointer to semaphore created (casted to \c void*).
*/
void * PMS_CreateSemaphore(int initialValue)
{
#ifdef MACOSX
  mac_sema_desc_t *sem;
  /* Mac OS X returns a failure for sem_init, so we have to use sem_open
     instead. sem_open requires a name, so we will invent one. */
  char name[SEM_NAME_LEN];
  sem_t *ossem;

  if ( (sem = OSMalloc(sizeof(mac_sema_desc_t), PMS_MemoryPoolPMS)) == NULL )
    return NULL;
  do {
    sprintf(name, "pms-%d-%d", getpid(), ++semno) ;
    if ( (ossem = sem_open(name, O_CREAT | O_EXCL, 0777, initialValue))
         == (sem_t *)SEM_FAILED )
      semno += 100; /* try to avoid other semaphores from the same crash */
  } while (ossem == (sem_t *)SEM_FAILED && errno == EEXIST);
  if (ossem == (sem_t *)SEM_FAILED) {
    OSFree(sem, PMS_MemoryPoolPMS);
    return NULL;
  }
  sem->sem = ossem; sem->semno = semno;
  sem_count++;
#else
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  sem_t *sem;

  sem = (sem_t *) OSMalloc(sizeof(sem_t), PMS_MemoryPoolPMS);

  if (sem == NULL)
    return NULL;
#else
  sem_t sem[16];
#endif
  if (sem_init(sem, 0, initialValue)) {
#ifdef PMS_OIL_MERGE_DISABLE_MEM
    OSFree(sem, PMS_MemoryPoolPMS);
#endif
    return NULL;
  }
#endif
  return (void*)sem;
}
Beispiel #9
0
__private_extern__ void ANControlListFree(ANControlList * list) {
    if (!list) return;
    ANControlListEntry * entry = list->first;
    while (entry) {
        ANControlListEntry * next = entry->next;
        ANControlListEntryFree(list->tag, entry);
        entry = next;
    }
    OSFree(list, sizeof(ANControlList), list->tag);
}
Beispiel #10
0
/*! \brief Start a new thread.
 *
 * UNIX/Linux
 *
 * \param start_address Funtion pointer to the threads entry point.
 * \param stack_size Size of stack to use, in bytes.
 * \param arglist Pointer to argument list.
 * \return Pointer to thread (casted to void * for platform portability)
 *
*/
void *PMS_BeginThread(void( *start_address )( void * ), unsigned int stack_size, void *arglist )
{
  pthread_attr_t attr;
  unsigned int result;
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  PMS_TyThreadFunction *ptThreadInfo = OSMalloc(sizeof(PMS_TyThreadFunction), PMS_MemoryPoolPMS);
#else
  PMS_TyThreadFunction *ptThreadInfo = mmalloc(sizeof(PMS_TyThreadFunction));
#endif

  if(!ptThreadInfo) {
    return (NULL);
  }

  ptThreadInfo->bCondBroadcasted = 0;

  if ( pthread_mutex_init(&ptThreadInfo->mutexThread, NULL) != 0 ) {
    goto thread_info_destroy;
  }

  if ( pthread_cond_init(&ptThreadInfo->condThread, NULL) != 0 ) {
    goto thread_mutex_destroy;
  }

  if (pthread_attr_init(&attr)) {
    goto thread_cond_destroy;
  }

  ptThreadInfo->start_routine = start_address;
  ptThreadInfo->arg = arglist;
  result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) ||
           pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) ||
           pthread_create(&ptThreadInfo->tid, &attr, PMSThreadWrapper, (void*)ptThreadInfo);

  (void)pthread_attr_destroy(&attr) ;

  if (result != 0) {
    PMS_SHOW_ERROR("Failed to create thread: \n");
    goto thread_cond_destroy;
  }

  return (void*)ptThreadInfo;

thread_cond_destroy:
  (void)pthread_cond_destroy(&ptThreadInfo->condThread) ;
thread_mutex_destroy:
  (void)pthread_mutex_destroy(&ptThreadInfo->mutexThread) ;
thread_info_destroy:
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  OSFree(ptThreadInfo, PMS_MemoryPoolPMS);
#else
  mfree(ptThreadInfo);
#endif
  return (NULL);
}
Beispiel #11
0
/**
 * \brief Close the file.
 *
 */
int File_CloseDataStream(void)
{
  /* File is already closed when the last byte was read. */

  if(l_pStoreBuffer)
  {
    OSFree(l_pStoreBuffer, PMS_MemoryPoolMisc);
    l_pStoreBuffer = NULL;
  }

  return TRUE;
}
Beispiel #12
0
// Resizes 'buffer' to 'size', preserving old content.
// Size must be larger than current buffer->size.
// Works by allocating a larger buffer and using memcpy. After this call,
// old buffer->buffer will be invalid memory.
// On error, returns 0 and deallocates everything, so buffer will be invalid.
kern_return_t pmem_resize(pmem_OSBuffer *buffer, uint32_t size) {
    char *newbuf = 0;
    if (!buffer) {
        goto error;
    }

    if (size < buffer->size) {
        // We're already bigger. That's fine - nothing to do.
        return KERN_SUCCESS;
    }

    newbuf = (char *)OSMalloc(size, buffer->tag);
    if (!newbuf) {
        goto error;
    }
    bzero(newbuf, size);

    memcpy(newbuf, buffer->buffer, buffer->size);
    OSFree(buffer->buffer, buffer->size, buffer->tag);
    buffer->buffer = newbuf;
    buffer->size = size;

    return KERN_SUCCESS;

error:
    if (newbuf) {
        OSFree(newbuf, size, buffer->tag);
    }

    if (buffer) {
        if (buffer->buffer) {
            OSFree(buffer->buffer, buffer->size, buffer->tag);
        }
        OSFree(buffer, sizeof(pmem_OSBuffer), buffer->tag);
    }

    return KERN_FAILURE;
}
Beispiel #13
0
/***********************************************************************************************************
*Function Name: GPS_BinInfo()
*Attributes:
*client  IN   Client identifier
*num     IN Number of bininfo[] array. Set the number of output bins obtained by gpsGetSysInfo().
*bininfo OUT Output Bin Information Structure
*bininfo_num OUT Returns the number of bins set to bininfo[] array.
*notify  IN Enable/disable receiving an event whenever there is a change in the information.
*Description: This function obtains output bin information.
************************************************************************************************************/
int GPS_BinInfo()
{
  long binInfo;
  int nTrayIndex;
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  gpsBinInfo = (gps_bininfo_t*) OSMalloc(sysinfo.num_bin * sizeof(gps_bininfo_t), PMS_MemoryPoolPMS);
  g_pstOutputInfo = (PMS_TyOutputInfo*) OSMalloc(sysinfo.num_bin * sizeof(PMS_TyOutputInfo), PMS_MemoryPoolPMS);
#else
  gpsBinInfo = (gps_bininfo_t*) mmalloc(sysinfo.num_bin * sizeof(gps_bininfo_t));
  g_pstOutputInfo = (PMS_TyOutputInfo*) mmalloc(sysinfo.num_bin * sizeof(PMS_TyOutputInfo));
#endif

  if( -1 == gpsGetBinInfo(gps_client, sysinfo.num_bin, gpsBinInfo, &binInfo, GPS_NOTIFY_CHANGE_OFF) )
    return -1;
  
  /* To do - Map SDK str from GPS Specification DB - 01.pdf  : page no 40	*/
  /* gpsGetTrayInfo - page no 200	*/
  
  for(nTrayIndex = 0; nTrayIndex < binInfo; nTrayIndex++)
  {
    /*g_pstOutputInfo[nTrayIndex].eOutputTray = gpsBinInfo[nTrayIndex].id;	*/
	switch (gpsBinInfo[nTrayIndex].id)
	{
	  case 1:
	  case 2:
	  case 3:
	    g_pstOutputInfo[nTrayIndex].eOutputTray = gpsBinInfo[nTrayIndex].id;
	    break;
	  default:
	    g_pstOutputInfo[nTrayIndex].eOutputTray = PMS_OUTPUT_TRAY_AUTO;
	    break;
      /* yet to map  PMS_OUTPUT_TRAY_UPPER, PMS_OUTPUT_TRAY_LOWER, PMS_OUTPUT_TRAY_EXTRA	*/
	}
    g_pstOutputInfo[nTrayIndex].nPriority = nTrayIndex;	/*assumption yet to conform. 	*/
	
  }

  g_nOutputTrays = sysinfo.num_bin;
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  OSFree( gpsBinInfo, PMS_MemoryPoolPMS );
#else
  mfree( gpsBinInfo );
#endif
  gpsBinInfo = NULL;
  
  return 0;
}
Beispiel #14
0
void
    rpal_memory_free
    (
        void* ptr
    )
{
    unsigned char* realPtr = ptr;
    uint32_t* pSize = NULL;
    
    if( NULL != ptr )
    {
        realPtr -= sizeof( uint32_t );
        pSize = (uint32_t*)realPtr;
        
        OSFree( realPtr, *pSize, g_mem_tag );
    }
}
Beispiel #15
0
//call back for detach
// ->just free socket's cookie
static void detach(void *cookie, socket_t so)
{
    //dbg msg
    IOLog("LULU: in %s\n", __FUNCTION__);
    
    //free cookie
    if(NULL != cookie)
    {
        //free
        OSFree(cookie, sizeof(struct cookieStruct), allocTag);
        
        //reset
        cookie = NULL;
    }
    
    return;
}
Beispiel #16
0
__private_extern__ errno_t ANControlListEntryAppend(OSMallocTag tag, ANControlListEntry * entry, mbuf_t packet) {
    uint32_t packSize = (uint32_t)mbuf_len(packet);
    if (packSize == 0) return 0;
    
    uint32_t newSize = packSize + entry->bufferLength;
            
    char * buffer = (char *)OSMalloc(newSize, tag);
    if (!buffer) return ENOMEM;
    
    if (entry->packetBuffer) {
        // copy old data to the buffer
        memcpy(buffer, entry->packetBuffer, entry->bufferLength);
        OSFree(entry->packetBuffer, entry->bufferLength, tag);
    }
    
    mbuf_copydata(packet, 0, packSize, &buffer[entry->bufferLength]);
    entry->packetBuffer = buffer;
    entry->bufferLength = newSize;
    return 0;
}
/**
 * \brief Cleanup all input modules.
 */
void PMS_IM_Finalize(){
	struct input_modules *pMods;
	struct input_modules *pModsNext;

#ifdef PMS_INPUT_IS_FILE
	/* File_Finalize() ... not implemented as it's not required */
#endif

#ifdef PMS_SUPPORT_SOCKET
	Input_Socket_Finalize();
	Socket_Finalize();
#endif

#ifndef PMS_OIL_MERGE_DISABLE_JS
#ifdef PMS_SUPPORT_BUFFER
	Input_Buffer_Finalize();
#endif
#endif

#ifdef PMS_HOT_FOLDER_SUPPORT
	HotFolder_Finalize();
#endif

	PMS_ASSERT(l_ptActive==NULL, ("PMS_IM_Finalize: Still got an active data stream\n"));

	for(pMods = l_ptModules; pMods; pMods = pModsNext) {
		/* initialize streams */
		pModsNext = pMods->pNext;
#ifdef PMS_OIL_MERGE_DISABLE_MEM
		OSFree(pMods, PMS_MemoryPoolPMS);
#else
		mfree(pMods);
#endif
	}
	l_ptModules = NULL;
	l_ptModulesTail = l_ptModules;
}
static
void ppfilter_detach(void *cookie, __unused socket_t so)
{
    if (cookie)
        OSFree(cookie, sizeof(struct pp_filter_cookie), pp_malloc_tag);
}
Beispiel #19
0
__private_extern__ void ANControlListEntryFree(OSMallocTag tag, ANControlListEntry * entry) {
    if (entry->packetBuffer) {
        OSFree(entry->packetBuffer, entry->bufferLength, tag);
    }
    OSFree(entry, sizeof(ANControlListEntry), tag);
}
Beispiel #20
0
/*! \brief Close and free a thread.
 *
 * UNIX/Linux
 *
 * \param[in] pThread Pointer to thread created by PMS_BeginThread.
 * \param[in] nWaitForThreadToExit Timeout in milliseconds.
 *              Negative: wait forever or until thread exits.
 *              Zero: do not wait.
 *              Postive: wait for up to # milliseconds or until thread exits.
 * \return 0: failure, 1: success, -1: success, but timeout occured whilst waiting for thread to exit.
*/
int PMS_CloseThread(void *pThread, int nWaitForThreadToExit)
{
  PMS_TyThreadFunction *pTInfo = pThread;
  int nRetVal = 0;

  if(!pTInfo)
    return 0;

  if(nWaitForThreadToExit > 0) {

    struct timeval now;
    struct timespec ts;
    int e;

    gettimeofday(&now, NULL);
    ts.tv_sec = now.tv_sec;
    ts.tv_nsec = now.tv_usec*1000;

    ts.tv_sec += nWaitForThreadToExit / 1000;

    ts.tv_nsec += nWaitForThreadToExit % 1000 * 1000000;

    if (ts.tv_nsec > 999999999){
      ts.tv_sec++;
      ts.tv_nsec = ts.tv_nsec % 1000000000;
    }

    /* wait (with timeout) until thread has finished */
    pthread_mutex_lock(&pTInfo->mutexThread);

    /* If the thread has already broadcasted the cond signal then we
       shouldn't wait for another one... there won't be another one. */
    if(pTInfo->bCondBroadcasted == 1) {
      e = 0;
    } else {
      e = pthread_cond_timedwait(&pTInfo->condThread, &pTInfo->mutexThread, &ts);
      pthread_mutex_unlock(&pTInfo->mutexThread);
    }

    /* now join so we wait until thread has -really- finished */
    if (e == ETIMEDOUT) {
      PMS_SHOW_ERROR("Timed out waiting for thread to exit.\n");
      (void)pthread_cancel(pTInfo->tid); /* try to forcefully stop it at
                                            a cancellation point */
      nRetVal = -1;
    } else {
      (void)pthread_join(pTInfo->tid, NULL);
      nRetVal = 1;
    }
  }
  else if (nWaitForThreadToExit < 0) {
    (void)pthread_join(pTInfo->tid, NULL) ;
    nRetVal = 1;
  }
  else {
    (void)pthread_cancel(pTInfo->tid); /* try to forcefully stop it at
                                          a cancellation point */
    nRetVal = 1;
  }

  (void)pthread_cond_destroy(&pTInfo->condThread) ;
  (void)pthread_mutex_destroy(&pTInfo->mutexThread) ;
  OSFree(pTInfo, PMS_MemoryPoolPMS);

  return nRetVal;
}
Beispiel #21
0
/**
 * \brief Open the next file.
 *
 * Opens the file contaning next job data.\n
 */
int File_OpenDataStream(void)
{
  /* PMS_SHOW("File_OpenDataStream()\n"); */

  if(!GetNextFilename(szJobFilename))
    return -1; /* -1 means don't try opening this module again */

  if ( (l_fileCurrent = fopen((char *)szJobFilename, "rb") ) == NULL )
  {
    PMS_SHOW_ERROR(" ***ASSERT*** File_OpenDataStream: Failed to open file %s \n", szJobFilename);
    return 0; /* 0 means you may try this module again to try the next job */
  }

  if(g_tSystemInfo.nStoreJobBeforeRip)
  {
    int nRead;
    int nChunk = 16 * 1024;
    PMS_SHOW("Storing job before passing to rip...\n");
    l_pStoreBuffer = OSMalloc(g_tSystemInfo.cbReceiveBuffer,PMS_MemoryPoolMisc);
    if(!l_pStoreBuffer) {
      PMS_SHOW_ERROR(" ***ASSERT*** File_OpenDataStream: Failed to allocate buffer for storing job\n");
      fclose(l_fileCurrent);
      l_fileCurrent = NULL;
      return 0; /* 0 means you may try this module again to try the next job */
    }
    fseek(l_fileCurrent,0,SEEK_END);
    l_nStoreBufferUsed = ftell(l_fileCurrent);
    fseek(l_fileCurrent,0,SEEK_SET);

    if(l_nStoreBufferUsed > g_tSystemInfo.cbReceiveBuffer)
    {
      PMS_SHOW_ERROR(" ***ASSERT*** File_OpenDataStream: Failed to store job. Job size is larger than the memory buffer.\n");
      fclose(l_fileCurrent);
      l_fileCurrent = NULL;
      OSFree(l_pStoreBuffer, PMS_MemoryPoolMisc);
      l_pStoreBuffer = NULL;
      return 0; /* 0 means you may try this module again to try the next job */
    }

    l_nStoreBufferUsed = 0;
    l_nStoreBufferPos = 0;
    do
    {
      if((l_nStoreBufferUsed + nChunk) < g_tSystemInfo.cbReceiveBuffer)
      {
        nRead = (int)fread(l_pStoreBuffer + l_nStoreBufferUsed, 1, nChunk, l_fileCurrent);
      }
      else if(l_nStoreBufferUsed < g_tSystemInfo.cbReceiveBuffer)
      {
        nRead = (int)fread(l_pStoreBuffer + l_nStoreBufferUsed, 1, g_tSystemInfo.cbReceiveBuffer - l_nStoreBufferUsed, l_fileCurrent);
      }
      else
      {
        nRead = 0;
      }
      l_nStoreBufferUsed += nRead;
    } while (nRead>0);

    fclose(l_fileCurrent);
    l_fileCurrent = NULL;
    PMS_SHOW("Stored %d bytes.\n", l_nStoreBufferUsed);
  }

  return 1; /* 1 means we have a job ready to rip */
}
Beispiel #22
0
// Free a meta struct.
void pmem_metafree(pmem_meta_t *meta) {
    OSFree(meta, meta->size, pmem_alloc_tag);
}
Beispiel #23
0
/*
  * Removes a procfsnode_t from its owning hash bucket and
  * releases its memory. This method must be called with the
  * hash table lock held.
  */
STATIC void
procfsnode_free_node(procfsnode_t *procfsnode) {
    LIST_REMOVE(procfsnode, node_hash);
    OSFree(procfsnode, sizeof(procfsnode_t), procfs_osmalloc_tag);
}
Beispiel #24
0
/***********************************************************************************************************
*Function Name: GPS_TrayInfo()
*Attributes:
*client  IN   Client identifier
*num     IN   Number of the trayinfo[] arrays. Set the number of trays indicated in the information obtained
*                by gpsGetSysInfo() function.
*trayinfo OUT Input Tray Information Structure
*trayinfo_num OUT Returns the number of trays set to trayinfo[] array.
*notify IN Set to receive / not receive event whenever there is a change in the information.
*Description: This function obtains input tray information.
************************************************************************************************************/
int GPS_TrayInfo()
{
  long trayNum;
  int nTrayIndex;

#define YET_TO_FIND_0 200
#define YET_TO_FIND_1 201
#define YET_TO_FIND_2 202
#define YET_TO_FIND_3 203
#define YET_TO_FIND_4 204
#define YET_TO_FIND_5 205
#define YET_TO_FIND_6 206
#define YET_TO_FIND_7 207

#ifdef PMS_OIL_MERGE_DISABLE_MEM
  gpsTrayInfo = (gps_trayinfo_t*) OSMalloc(sysinfo.num_tray * sizeof(gps_trayinfo_t), PMS_MemoryPoolPMS);
  g_pstTrayInfo = (PMS_TyTrayInfo*) OSMalloc(sysinfo.num_tray * sizeof(PMS_TyTrayInfo), PMS_MemoryPoolPMS);
#else
  gpsTrayInfo = (gps_trayinfo_t*) mmalloc(sysinfo.num_tray * sizeof(gps_trayinfo_t));
  g_pstTrayInfo = (PMS_TyTrayInfo*) mmalloc(sysinfo.num_tray * sizeof(PMS_TyTrayInfo));
#endif

  if( -1 == gpsGetTrayInfo(gps_client, sysinfo.num_tray, gpsTrayInfo, &trayNum, GPS_NOTIFY_CHANGE_OFF) )
    return -1;
  /* To do - Map SDK str from GPS Specification DB - 01.pdf  : page no 37	*/
  /* gpsGetTrayInfo - page no 197	*/

  for(nTrayIndex = 0; nTrayIndex < trayNum; nTrayIndex++)
  {
    switch(gpsTrayInfo[nTrayIndex].id)
	{
	  case 0:
        g_pstTrayInfo[nTrayIndex].eMediaSource = PMS_TRAY_MANUALFEED;
		break;
      case 1:
        g_pstTrayInfo[nTrayIndex].eMediaSource = PMS_TRAY_TRAY1;
		break;
      case 2:
        g_pstTrayInfo[nTrayIndex].eMediaSource = PMS_TRAY_TRAY2;
		break;
      case 3:
        g_pstTrayInfo[nTrayIndex].eMediaSource = PMS_TRAY_TRAY3;
		break;
      default:
        g_pstTrayInfo[nTrayIndex].eMediaSource = PMS_TRAY_AUTO; 
		break;
		
		//Yet to map PMS_TRAY_BYPASS, PMS_TRAY_ENVELOPE
	}
	
	
	//Guess!!!!!!  the paper size.
	/*
	GPS_CODE_NO_PAPER = 0,
	GPS_CODE_A0,		GPS_CODE_A1,		GPS_CODE_A2,		GPS_CODE_A3,
	GPS_CODE_A4,		GPS_CODE_A5,		GPS_CODE_A6,		GPS_CODE_A7,
	GPS_CODE_B0,		GPS_CODE_B1,		GPS_CODE_B2,		GPS_CODE_B3,
	GPS_CODE_B4,		GPS_CODE_B5,		GPS_CODE_B6,		GPS_CODE_B7,
	GPS_CODE_WMAIL,		GPS_CODE_MAIL,		GPS_CODE_LINE1,		GPS_CODE_LINE2,
	GPS_CODE_LIB6,		GPS_CODE_LIB8,		GPS_CODE_210x170,	GPS_CODE_210x182,
	GPS_CODE_267x388,

	GPS_CODE_FREEmm = 31,
	GPS_CODE_11x17,
	GPS_CODE_11x14,		GPS_CODE_10x15,		GPS_CODE_10x14,		GPS_CODE_8Hx14,
	GPS_CODE_8Hx13,		GPS_CODE_8Hx11,		GPS_CODE_8Qx14,		GPS_CODE_8Qx13,
	GPS_CODE_8x13,		GPS_CODE_8x10H,		GPS_CODE_8x10,		GPS_CODE_5Hx8H,
	GPS_CODE_7Qx10H,

	GPS_CODE_12x18 = 47,
	GPS_CODE_12x14H,
	GPS_CODE_11x15,		GPS_CODE_9Hx11,		 GPS_CODE_8Hx12,	GPS_CODE_13x19,

	GPS_CODE_8KAI = 66,
	GPS_CODE_16KAI,

	GPS_CODE_NO_10 = 80,
	GPS_CODE_NO_7,

	GPS_CODE_C5 = 83,
	GPS_CODE_C6,		GPS_CODE_DL,

	GPS_CODE_NO_SIZE = 128,
	GPS_CODE_A0T,		GPS_CODE_A1T,		GPS_CODE_A2T,		GPS_CODE_A3T,
	GPS_CODE_A4T,		GPS_CODE_A5T,		GPS_CODE_A6T,		GPS_CODE_A7T,
	GPS_CODE_B0T,		GPS_CODE_B1T,		GPS_CODE_B2T,		GPS_CODE_B3T,
	GPS_CODE_B4T,		GPS_CODE_B5T,		GPS_CODE_B6T,		GPS_CODE_B7T,
	GPS_CODE_WMAILT,	GPS_CODE_MAILT,		GPS_CODE_LINE1T,	GPS_CODE_LINE2T,
	GPS_CODE_LIB6T,		GPS_CODE_LIB8T,		GPS_CODE_210x170T,	GPS_CODE_210x182T,
	GPS_CODE_267x388T,

	GPS_CODE_FREEmmT = 159,
	GPS_CODE_11x17T,
	GPS_CODE_11x14T,	GPS_CODE_10x15T,	GPS_CODE_10x14T,	GPS_CODE_8Hx14T,
	GPS_CODE_8Hx13T,	GPS_CODE_8Hx11T,	GPS_CODE_8Qx14T,	GPS_CODE_8Qx13T,
	GPS_CODE_8x13T,		GPS_CODE_8x10HT,	GPS_CODE_8x10T,		GPS_CODE_5Hx8HT,
	GPS_CODE_7Qx10HT,

	GPS_CODE_12x18T = 175,
	GPS_CODE_12x14HT,
	GPS_CODE_11x15T,	GPS_CODE_9Hx11T,	 GPS_CODE_8Hx12T,	GPS_CODE_13x19T,

	GPS_CODE_8KAIT = 194,
	GPS_CODE_16KAIT,

	GPS_CODE_NO_10T = 208,
	GPS_CODE_NO_7T,

	GPS_CODE_C5T = 211,
	GPS_CODE_C6T,		GPS_CODE_DL_T
	*/
	
	switch(gpsTrayInfo[nTrayIndex].p_size)
	{
	  case GPS_CODE_8Hx11:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LETTER;
	    break;
	  case GPS_CODE_A4:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A4;
	    break;
	  case GPS_CODE_8Hx14:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LEGAL;
	    break;
	  case GPS_CODE_7Qx10H:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_EXE;
	    break;
	  case GPS_CODE_A3:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A3;
	    break;
	  case GPS_CODE_11x17:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_TABLOID;
	    break;
	  case GPS_CODE_A5:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A5;
	    break;
	  case GPS_CODE_A6:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A6;
	    break;
	  case GPS_CODE_C5:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_C5_ENV;
	    break;
	  case GPS_CODE_DL:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_DL_ENV;
	    break;
	  case YET_TO_FIND_0:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LEDGER;
	    break;
	  case YET_TO_FIND_2:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_OFUKU;
	    break;
	  case GPS_CODE_10x14:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_JISB4;
	    break;
	  case YET_TO_FIND_3:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_JISB5;
	    break;
	  case GPS_CODE_8Hx11T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LETTER_R;
	    break;
	  case GPS_CODE_A4T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A4_R;
	    break;
	  case GPS_CODE_8Hx14T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LEGAL_R;
	    break;
	  case GPS_CODE_7Qx10HT:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_EXE_R;
	    break;
	  case GPS_CODE_A3T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A3_R;
	    break;
	  case GPS_CODE_11x17T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_TABLOID_R;
	    break;
	  case GPS_CODE_A5T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A5_R;
	    break;
	  case GPS_CODE_A6T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_A6_R;
	    break;
	  case GPS_CODE_C5T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_C5_ENV_R;
	    break;
	  case GPS_CODE_DL_T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_DL_ENV_R;
	    break;
	  case YET_TO_FIND_4:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_LEDGER_R;
	    break;
	  case YET_TO_FIND_5:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_OFUKU_R;
	    break;
	  case GPS_CODE_10x14T:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_JISB4_R;
	    break;
	  case YET_TO_FIND_6:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_JISB5_R;
	    break;
	  case YET_TO_FIND_7:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_CUSTOM;
	    break;
	  default:
	    g_pstTrayInfo[nTrayIndex].ePaperSize = PMS_SIZE_DONT_KNOW;
	    break;
	}
	
	switch(gpsTrayInfo[nTrayIndex].p_kind)
	{
	  case DI_PAPER_NORMAL:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_PLAIN;
	    break;
	  case DI_PAPER_BOND:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_BOND;
	    break;
	  case DI_PAPER_SPECIAL:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_SPECIAL;
	    break;
	  case DI_PAPER_GLOSSY:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_GLOSSY;
	    break;
	  case DI_PAPER_OHP:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_TRANSPARENCY;
	    break;
	  case DI_PAPER_RECYCLE:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_RECYCLED;
	    break;
	  case DI_PAPER_MIDDLETHICK:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_THICK;
	    break;
	  case DI_PAPER_ENVELOPE:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_ENVELOPE;
	    break;
	  case DI_PAPER_POSTCARD:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_POSTCARD;
	    break;
	  case DI_PAPER_THIN:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_THIN;
	    break;
	  case DI_PAPER_LABEL:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_LABEL;
	    break;
	  case DI_PAPER_PREPRINT:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_PREPRINTED;
	    break;
	  case DI_PAPER_LETTER_HEAD:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_LETTERHEAD;
	    break;
	  default:
	    g_pstTrayInfo[nTrayIndex].eMediaType = PMS_TYPE_DONT_KNOW;
	    break;
	}
	
	g_pstTrayInfo[nTrayIndex].eMediaColor = PMS_COLOR_DONT_KNOW; /* yet to map	*/
    g_pstTrayInfo[nTrayIndex].uMediaWeight = 0; /* yet to map	*/
    g_pstTrayInfo[nTrayIndex].nPriority = nTrayIndex; /* assumption yet to conform. 	*/
	
	g_pstTrayInfo[nTrayIndex].bTrayEmptyFlag = (GPS_TRAY_PAPEREND == gpsTrayInfo[nTrayIndex].status);
    g_pstTrayInfo[nTrayIndex].nNoOfSheets = gpsTrayInfo[nTrayIndex].remain;

  }
  
  g_nInputTrays = sysinfo.num_tray;
#ifdef PMS_OIL_MERGE_DISABLE_MEM
  OSFree( gpsTrayInfo, PMS_MemoryPoolPMS );
#else
  mfree( gpsTrayInfo );
#endif
  gpsTrayInfo = NULL;
    
  return 0;
}
Beispiel #25
0
/*
 * Finds the procfsnode_t for a node with a given id and referencing a given structure node
 * on a given instance of the file system. If the node does not already exist, it is created, 
 * entered into the node has table and a vnode is created and attached to it. If the node already 
 * exists, it is returned along with its vnode. In both cases, the vnode has an additional iocount,
 * which the caller must remove at some point by calling vnode_put().
 *
 * Creation of a vnode cannot be performed by this function because the information required 
 * to initialize it is known only to the caller. The caller must supply a pointer to a function
 * that will create the vnode when required, along with an opaque context pointer that is passed
 * to the creation function, along with a pointer to the corresponding procfsnode_t. The creation
 * function must either create the vnode and link it to the procfsnode_t or return an error.
 *
 * The allocation of the procfsnode_t that is done here is reversed in the procfs_reclaim() function,
 * which is called when the node's associated vnode is being reclaimed.
 */
int
procfsnode_find(procfs_mount_t *pmp, procfsnode_id_t node_id, procfs_structure_node_t *snode,
                procfsnode_t **pnpp, vnode_t *vnpp,
                create_vnode_func create_vnode_func,
                void *create_vnode_params) {
    int error = 0;
    boolean_t locked = TRUE;
    procfsnode_t *target_procfsnode = NULL;     // This is the node that we will return.
    procfsnode_t *new_procfsnode = NULL;        // Newly allocated node. Will be freed if not used.
    vnode_t target_vnode = NULL;                // Start by assuming we will not get a vnode.
    int32_t mount_id = pmp->pmnt_id;            // File system id.
    
    // Lock the hash table. We'll keep this locked until we are done,
    // unless we need to allocate memory. In that case, we'll drop the
    // lock, but we'll have to revisit all of our assumptions when we
    // reacquire it, because another thread may have created the node
    // we are looking for.
    lck_mtx_lock(procfsnode_hash_mutex);
    
    boolean_t done = FALSE;
    while (!done) {
        assert(locked);
        error = 0;
        
        // Select the correct hash bucket and walk along it, looking for an existing
        // node with the correct attributes.
        int nodehash = HASH_FOR_MOUNT_AND_ID(mount_id, node_id);
        procfs_hash_head *hash_bucket = PROCFS_NODE_HASH_TO_BUCKET_HEADER(nodehash);
        LIST_FOREACH(target_procfsnode, hash_bucket, node_hash) {
            if (target_procfsnode->node_mnt_id == mount_id
                    && target_procfsnode->node_id.nodeid_pid == node_id.nodeid_pid
                    &&target_procfsnode->node_id.nodeid_objectid == node_id.nodeid_objectid
                    && target_procfsnode->node_id.nodeid_base_id == node_id.nodeid_base_id) {
                // Matched.
                break;
            }
        }
        
        // We got a match if target_procfsnode is not NULL.
        if (target_procfsnode == NULL) {
            // We did not find a match, so either allocate a new node or use the
            // one we created last time around this loop.
            if (new_procfsnode == NULL) {
                // We need to allocate a new node. Before doing that, unlock
                // the node hash, because the memory allocation may block.
                lck_mtx_unlock(procfsnode_hash_mutex);
                locked = FALSE;
                
                new_procfsnode = (procfsnode_t *)OSMalloc(sizeof(procfsnode_t), procfs_osmalloc_tag);
                if (new_procfsnode == NULL) {
                    // Allocation failure - bail. Nothing to clean up and
                    // we don't hold the lock.
                    error = ENOMEM;
                    break;
                }
                
                // We got a new procfsnode. Relock the node hash, then go around the
                // loop again. This is necessary because someone else may have created
                // the same node after we dropped the lock. If that's the case, we'll
                // find that node next time around and we'll use it. The one we just
                // allocated will remain in target_procfsnode and will be freed before we return.
                lck_mtx_lock(procfsnode_hash_mutex);
                locked = TRUE;
                continue;
            } else {
                // If we get here, we know that we need to use the node that we
                // allocated last time around the loop, so promote it to target_procfsnode
                assert(locked);
                assert(new_procfsnode != NULL);
                
                target_procfsnode = new_procfsnode;
                
                // Initialize the new node.
                memset(target_procfsnode, 0, sizeof(procfsnode_t));
                target_procfsnode->node_mnt_id = mount_id;
                target_procfsnode->node_id = node_id;
                target_procfsnode->node_structure_node = snode;
                
                // Add the node to the node hash. We already know which bucket
                // it belongs to.
                LIST_INSERT_HEAD(hash_bucket, target_procfsnode, node_hash);
            }
        }
        
        // At this point, we have a procfsnode_t, which either already existed
        // or was just created. We also have the lock for the node hash table.
        assert(target_procfsnode != NULL);
        assert(locked);
        
        // Check whether another thread is already in the process of creating a
        // vnode for this procfsnode_t. If it is, wait until it's done and go
        // around the loop again.
        if (target_procfsnode->node_attaching_vnode) {
            // Indicate that a wakeup is needed when the attaching thread
            // is done.
            target_procfsnode->node_thread_waiting_attach = TRUE;
            
            // Sleeping will drop and relock the mutex.
            msleep(target_procfsnode, procfsnode_hash_mutex, PINOD, "procfsnode_find", NULL);
            
            // Since anything can have changed while we were away, go around
            // the loop again.
            continue;
        }
        
        target_vnode = target_procfsnode->node_vnode;
        if (target_vnode != NULL) {
            // We already have a vnode. We need to check if it has been reassigned.
            // To do that, unlock and check the vnode id.
            uint32_t vid = vnode_vid(target_vnode);
            lck_mtx_unlock(procfsnode_hash_mutex);
            locked = FALSE;
            
            error = vnode_getwithvid(target_vnode, vid);
            if (error != 0) {
                // Vnode changed identity, so we need to redo everything. Relock
                // because we are expected to hold the lock at the top of the loop.
                // Getting here means that the vnode was reclaimed and the procfsnode
                // was removed from the hash and freed, so we will be restarting from scratch.
                lck_mtx_lock(procfsnode_hash_mutex);
                target_procfsnode = NULL;
                new_procfsnode = NULL;
                locked = TRUE;
                continue;
            }
            
            // The vnode was still present and has not changed id. All we need to do
            // is terminate the loop. We don't hold the lock, "locked" is FALSE and
            // we don't need to relock (and indeed doing so would introduce yet more
            // race conditions). vnode_getwithvid() added an iocount reference for us,
            // which the caller is expected to eventually release with vnode_put().
            assert(error == 0);
            break;
        }
        
        // At this point, we have procfsnode_t in the node hash, but we don't have a
        // vnode. To create the vnode, we have to release the node hash lock and invoke
        // the caller's create_vnode_func callback. Before doing that, we need to set
        // node_attaching_vnode to force any other threads that come in here to wait for
        // this thread to create the vnode (or fail).
        target_procfsnode->node_attaching_vnode = TRUE;
        lck_mtx_unlock(procfsnode_hash_mutex);
        locked = FALSE;
        
        error = (*create_vnode_func)(create_vnode_params, target_procfsnode, &target_vnode);
        assert(error != 0 || target_vnode != NULL);
        
        // Relock the hash table and clear node_attaching_vnode now that we are
        // safely back from the caller's callback.
        lck_mtx_lock(procfsnode_hash_mutex);
        locked = TRUE;
        target_procfsnode->node_attaching_vnode = FALSE;
        
        // If there are threads waiting for the vnode attach to complete,
        // wake them up.
        if (target_procfsnode->node_thread_waiting_attach) {
            target_procfsnode->node_thread_waiting_attach = FALSE;
            wakeup(target_procfsnode);
        }
        
        // Now check whether we succeeded.
        if (error != 0) {
            // Failed to create the vnode -- this is fatal.
            // Remove the procfsnode_t from the hash table and
            // release it.
            procfsnode_free_node(target_procfsnode);
            new_procfsnode = NULL; // To avoid double free.
            break;
        }
        
        // We got the new vnode and it's already linked to the procfsnode_t.
        // Link the procfsnode_t to it. Also add a file system reference to
        // the vnode itself.
        target_procfsnode->node_vnode = target_vnode;
        vnode_addfsref(target_vnode);
        
        break;
    }
    
    // Unlock the hash table, if it is still locked.
    if (locked) {
        lck_mtx_unlock(procfsnode_hash_mutex);
    }
    
    // Free the node we allocated, if we didn't use it. We do this
    // *after* releasing the hash lock just in case it might block.
    if (new_procfsnode != NULL && new_procfsnode != target_procfsnode) {
        OSFree(new_procfsnode, sizeof(procfsnode_t), procfs_osmalloc_tag);
    }
    
    // Set the return value, or NULL if we failed.
    *pnpp = error == 0 ? target_procfsnode : NULL;
    *vnpp = error == 0 ? target_vnode : NULL;
    
    return error;
}