示例#1
0
文件: buffer.c 项目: hhexiy/scip
/** reallocates the buffer to at least the given size */
SCIP_RETCODE SCIPbufferReallocMem(
   SCIP_BUFFER*          buffer,             /**< memory buffer storage */
   SCIP_SET*             set,                /**< global SCIP settings */
   void**                ptr,                /**< pointer to the allocated memory buffer */
   int                   size                /**< minimal required size of the buffer */
   )
{
#ifndef SCIP_NOBUFFERMEM
   int bufnum;

   assert(buffer != NULL);
   assert(buffer->firstfree <= buffer->ndata);
   assert(ptr != NULL);
   assert(size >= 0);

   /* if the pointer doesn't exist yet, allocate it */
   if( *ptr == NULL )
      return SCIPbufferAllocMem(buffer, set, ptr, size);

   assert(buffer->firstfree >= 1);

   /* Search the pointer in the buffer list
    * Usually, buffers are allocated and freed like a stack, such that the currently used pointer is
    * most likely at the end of the buffer list.
    */
   for( bufnum = buffer->firstfree-1; bufnum >= 0 && buffer->data[bufnum] != *ptr; --bufnum )
   {
   }
   assert(bufnum >= 0);
   assert(buffer->data[bufnum] == *ptr);
   assert(buffer->used[bufnum]);
   assert(buffer->size[bufnum] >= 1);

   /* check if the buffer has to be enlarged */
   if( size > buffer->size[bufnum] )
   {
      int newsize;

      /* enlarge buffer */
      newsize = SCIPsetCalcMemGrowSize(set, size);
      SCIP_ALLOC( BMSreallocMemorySize(&buffer->data[bufnum], newsize) );
      buffer->size[bufnum] = newsize;
      *ptr = buffer->data[bufnum];
   }
   assert(buffer->size[bufnum] >= size);
   assert(*ptr == buffer->data[bufnum]);

   SCIPdebugMessage("reallocated buffer %d/%d at %p to size %d (required size: %d) for pointer %p\n", 
      bufnum, buffer->ndata, buffer->data[bufnum], buffer->size[bufnum], size, (void*)ptr);

#else
   SCIP_ALLOC( BMSreallocMemorySize(ptr, size) );
#endif

   return SCIP_OKAY;
}
示例#2
0
/** prints warning message with the current message handler, or buffers the message if no newline exists */
static
void messagePrintWarning(
   SCIP_MESSAGEHDLR*     messagehdlr,        /**< message handler */
   const char*           msg,                /**< message to print; NULL to flush the output buffer */
   int                   msglength           /**< message length if bigger than SCIP_MAXSTRLEN, or SCIP_MAXSTRLEN */
   )
{
   if( messagehdlr != NULL && messagehdlr->messagewarning != NULL && (!messagehdlr->quiet || messagehdlr->logfile != NULL) )
   {
      char* outmsg;
      int outmsgsize;

      outmsgsize = msglength + 1;

      if( BMSallocMemorySize(&outmsg, outmsgsize) == NULL )
         return;

      if( !bufferMessage(messagehdlr->warningbuffer, &messagehdlr->warningbufferlen, msg, outmsg, &outmsgsize) )
      {
         assert(outmsgsize > msglength + 1);
         if( BMSreallocMemorySize(&outmsg, outmsgsize) != NULL )
         {
#ifndef NDEBUG
            SCIP_Bool ret;

            ret = bufferMessage(messagehdlr->warningbuffer, &messagehdlr->warningbufferlen, msg, outmsg, &outmsgsize);
            assert(ret);
#else
            bufferMessage(messagehdlr->warningbuffer, &messagehdlr->warningbufferlen, msg, outmsg, &outmsgsize);
#endif
         }
         else
         {
            BMSfreeMemory(&outmsg);
            return;
         }
      }

      if( *outmsg != '\0' )
      {
         if( !messagehdlr->quiet )
            messagehdlr->messagewarning(messagehdlr, stderr, outmsg);
         if( messagehdlr->logfile != NULL )
            messagehdlr->messagewarning(messagehdlr, messagehdlr->logfile, outmsg);
      }

      BMSfreeMemory(&outmsg);
   }
}
示例#3
0
文件: buffer.c 项目: hhexiy/scip
/** allocates the next unused buffer */
SCIP_RETCODE SCIPbufferAllocMem(
   SCIP_BUFFER*          buffer,             /**< memory buffer storage */
   SCIP_SET*             set,                /**< global SCIP settings */
   void**                ptr,                /**< pointer to store the allocated memory buffer */
   int                   size                /**< minimal required size of the buffer */
   )
{
#ifndef SCIP_NOBUFFERMEM
   int bufnum;

   assert(buffer != NULL);
   assert(buffer->firstfree <= buffer->ndata);
   assert(ptr != NULL);
   assert(size >= 0);
   
   /* allocate minimal 1 byte */
   if( size == 0 )
      size = 1;

   /* check, if we need additional buffers */
   if( buffer->firstfree == buffer->ndata )
   {
      int newsize;
      int i;

      /* create additional buffers */
      newsize = SCIPsetCalcMemGrowSize(set, buffer->firstfree+1);
      SCIP_ALLOC( BMSreallocMemoryArray(&buffer->data, newsize) );
      SCIP_ALLOC( BMSreallocMemoryArray(&buffer->size, newsize) );
      SCIP_ALLOC( BMSreallocMemoryArray(&buffer->used, newsize) );
      for( i = buffer->ndata; i < newsize; ++i )
      {
         buffer->data[i] = NULL;
         buffer->size[i] = 0;
         buffer->used[i] = FALSE;
      }
      buffer->ndata = newsize;
   }
   assert(buffer->firstfree < buffer->ndata);

   /* check, if the current buffer is large enough */
   bufnum = buffer->firstfree;
   assert(!buffer->used[bufnum]);
   if( buffer->size[bufnum] < size )
   {
      int newsize;

      /* enlarge buffer */
      newsize = SCIPsetCalcMemGrowSize(set, size);
      SCIP_ALLOC( BMSreallocMemorySize(&buffer->data[bufnum], newsize) );
      buffer->size[bufnum] = newsize;
   }
   assert(buffer->size[bufnum] >= size);

   *ptr = buffer->data[bufnum];
   buffer->used[bufnum] = TRUE;
   buffer->firstfree++;

   SCIPdebugMessage("allocated buffer %d/%d at %p of size %d (required size: %d) for pointer %p\n", 
      bufnum, buffer->ndata, buffer->data[bufnum], buffer->size[bufnum], size, (void*)ptr);

#else
   SCIP_ALLOC( BMSallocMemorySize(ptr, size) );
#endif

   return SCIP_OKAY;
}
示例#4
0
/** prints info message with the current message handler, or buffers the message if no newline exists */
static
void messagePrintInfo(
   SCIP_MESSAGEHDLR*     messagehdlr,        /**< message handler */
   FILE*                 file,               /**< file stream to print into, or NULL for stdout */
   const char*           msg,                /**< message to print; NULL to flush the output buffer */
   int                   msglength           /**< message length if bigger than SCIP_MAXSTRLEN, or SCIP_MAXSTRLEN */
   )
{
   if( messagehdlr != NULL && messagehdlr->messageinfo != NULL )
   {
      if( (file == NULL || file == stdout) && !messagehdlr->quiet && (msg == NULL || strlen(msg) < SCIP_MAXSTRLEN) )
      {
         char* outmsg;
         int outmsgsize;

         outmsgsize = msglength + 1;

         if( BMSallocMemorySize(&outmsg, outmsgsize) == NULL )
            return;

         if( !bufferMessage(messagehdlr->infobuffer, &messagehdlr->infobufferlen, msg, outmsg, &outmsgsize) )
         {
            assert(outmsgsize > msglength + 1);
            if( BMSreallocMemorySize(&outmsg, outmsgsize) != NULL )
            {
#ifndef NDEBUG
               SCIP_Bool ret;

               ret = bufferMessage(messagehdlr->infobuffer, &messagehdlr->infobufferlen, msg, outmsg, &outmsgsize);
               assert(ret);
#else
               bufferMessage(messagehdlr->infobuffer, &messagehdlr->infobufferlen, msg, outmsg, &outmsgsize);
#endif
            }
            else
            {
               BMSfreeMemory(&outmsg);
               return;
            }
         }

         if( *outmsg != '\0' )
         {
            messagehdlr->messageinfo(messagehdlr, stdout, outmsg);

            if( messagehdlr->logfile != NULL )
               messagehdlr->messageinfo(messagehdlr, messagehdlr->logfile, outmsg);
         }

         BMSfreeMemory(&outmsg);
      }
      else if( msg != NULL )
      {
         /* file output cannot be buffered because the output file may change or the message is to long */
         if( *msg != '\0' )
         {
            if( !messagehdlr->quiet || (file != NULL && file != stdout) )
               messagehdlr->messageinfo(messagehdlr, file == NULL ? stdout : file, msg);
            if( messagehdlr->logfile != NULL && (file == NULL || file == stdout) )
               messagehdlr->messageinfo(messagehdlr, messagehdlr->logfile, msg);
         }
      }
   }
}