示例#1
0
文件: heap.c 项目: cloklo/swoole-src
void* swHeap_push(swHeap *heap, uint64_t priority, void *data)
{
    void *tmp;
    uint32_t i;
    uint32_t newsize;

    if (heap->num >= heap->size)
    {
        newsize = heap->size * 2;
        if (!(tmp = sw_realloc(heap->nodes, sizeof(void *) * newsize)))
        {
            return NULL;
        }
        heap->nodes = tmp;
        heap->size = newsize;
    }

    swHeap_node *node = sw_malloc(sizeof(swHeap_node));
    if (!node)
    {
        return NULL;
    }
    node->priority = priority;
    node->data = data;
    i = heap->num++;
    heap->nodes[i] = node;
    swHeap_bubble_up(heap, i);
    return node;
}
示例#2
0
int swString_extend(swString *str, size_t new_size)
{
    assert (new_size > str->size);
    str->str = sw_realloc(str->str, new_size);
    if (str->str == NULL)
    {
        swWarn("realloc failed.");
        return SW_ERR;
    }
    str->size = new_size;
    return SW_OK;
}
示例#3
0
int swString_extend(swString *str, size_t new_size)
{
    assert(new_size > str->size);
    char *new_str = sw_realloc(str->str, new_size);
    if (new_str == NULL)
    {
        swSysError("realloc(%ld) failed.", new_size);
        return SW_ERR;
    }
    str->str = new_str;
    str->size = new_size;
    return SW_OK;
}
示例#4
0
文件: string.c 项目: how9s/swoole
int swString_extend(swString *str, size_t new_size)
{
    if(new_size <= str->size)
    {
        swWarn("new_size <= size. extend failed.");
        return SW_ERR;
    }
    str->str = sw_realloc(str->str, new_size);
    if (str->str == NULL)
    {
        swWarn("realloc failed.");
        return SW_ERR;
    }
    return SW_OK;
}
示例#5
0
void swSignalfd_add(int signo, __sighandler_t callback)
{
	if (object.num == object.size)
	{
		object.items = sw_realloc(object.items, sizeof(swSignal_item) * object.size * 2);
		if (object.items == NULL)
		{
			swError("realloc for swSignal_item failed.");
			return;
		}
		object.size = object.size * 2;
	}
	sigaddset(&swoole_signalfd_mask, signo);
	object.items[object.num].callback = callback;
	object.items[object.num].signo = signo;
	object.num ++;
}
示例#6
0
sw_result
sw_corby_channel_recv(
						sw_corby_channel		self,
						sw_salt				*	salt,
						sw_corby_message	*	message,
						sw_uint32			*	request_id,
						sw_string			*	op,
						sw_uint32			*	op_len,
						sw_corby_buffer	*	buffer,
						sw_uint8			*	endian,
						sw_bool				block)
{
	static sw_const_string
	message_type[] =
	{
		"Request",
		"Reply",
		"CancelRequest",
		"LocateRequest",
		"LocateReply",
		"CloseConnection",
		"MessageError"
	};

	sw_size_t	buflen;
	sw_size_t	len;
	sw_result	err = SW_OKAY;

	SW_UNUSED_PARAM(request_id);

   sw_debug(SW_LOG_VERBOSE, "entering sw_corby_channel_recv()\n");

	if (self->m_state == Waiting)
	{
		self->m_message->m_header = NULL;
	}

	/*
	 * side effect the salt reference
	 */
	if (salt)
	{
		(*salt) = self->m_orb->m_salt;
	}
   
   while (SW_TRUE)
   {
      buflen 	= (self->m_read_buffer->m_eptr - self->m_read_buffer->m_bptr);
 		*buffer	= NULL;
		
		if (message)
   	{
			*message = NULL;
  		}
   
		sw_debug(SW_LOG_VERBOSE, "  %s %s, buffer(m_base = %x, m_bptr = %x, m_eptr = %x, m_end = %x, buflen = %d)\n",
                     (block) ? "block" : "!block",
                     (self->m_message->m_header) ? "message_header" : "!message_header",
                     self->m_read_buffer->m_base,
                     self->m_read_buffer->m_bptr,
                     self->m_read_buffer->m_eptr,
                     self->m_read_buffer->m_end,
                     buflen);
      
      /*
         beginning a new message
      */
      if (self->m_message->m_header == NULL)
      {
			self->m_state = Reading;

         /*
            do we need to shift stuff down and reset pointers?
         */
         if (buflen && (self->m_read_buffer->m_bptr != self->m_read_buffer->m_base))
         {
				sw_debug(SW_LOG_VERBOSE, "  shifting buffer pointers %d bytes\n", buflen);
				sw_memcpy(self->m_read_buffer->m_base, self->m_read_buffer->m_bptr, buflen);
         }

         self->m_read_buffer->m_bptr = self->m_read_buffer->m_base;
         self->m_read_buffer->m_eptr = self->m_read_buffer->m_base + buflen;
         
         /*
            do we have enough to read a message header
         */
         if (buflen >= sizeof(struct _sw_swop_message_header))
         {
            self->m_message->m_header = (sw_swop_message_header*) self->m_read_buffer->m_base;

				sw_debug(SW_LOG_VERBOSE, "  SWOP magic = %c %c %c %c\n", 
                     self->m_message->m_header->m_magic[0],
                     self->m_message->m_header->m_magic[1],
                     self->m_message->m_header->m_magic[2],
                     self->m_message->m_header->m_magic[3]);

            /*
               check magic string
            */
            sw_check
						(
						((self->m_message->m_header->m_magic[0] == 'S') &&
						 (self->m_message->m_header->m_magic[1] == 'W') &&
						 (self->m_message->m_header->m_magic[2] == 'O') &&
						 (self->m_message->m_header->m_magic[3] == 'P')),
						exit,
						err = SW_E_CORBY_BAD_MESSAGE
						);
            
            sw_debug(SW_LOG_VERBOSE, "  SWOP version = %d %d\n", 
                     self->m_message->m_header->m_major,
                     self->m_message->m_header->m_minor);

            /*
               check version number
            */
				sw_check
						(
						((self->m_message->m_header->m_major <= SW_SWOP_MAJOR) &&
						 (self->m_message->m_header->m_minor <= SW_SWOP_MINOR)),
						exit,
						err = SW_E_CORBY_BAD_VERSION
						);
						
            sw_debug(SW_LOG_VERBOSE, "  SWOP endian = %d\n", 
                     self->m_message->m_header->m_endian);

				sw_debug(SW_LOG_VERBOSE, "  SWOP message type = %s\n", message_type[self->m_message->m_header->m_msg_type]);

            /*
               fix size
            */
            if (self->m_message->m_header->m_endian != SW_ENDIAN)
            {
               self->m_message->m_header->m_msg_size = SW_SWAP32(self->m_message->m_header->m_msg_size);
            }

            sw_debug(SW_LOG_VERBOSE, "  SWOP size = %d\n", 
                     self->m_message->m_header->m_msg_size);
 
            /*
               make sure we have enough buffer space
            */
            if (self->m_message->m_header->m_msg_size > ((self->m_read_buffer->m_end - self->m_read_buffer->m_base) - sizeof(struct _sw_swop_message_header)))
            {
               sw_uint32 new_size;
               
               new_size								= self->m_message->m_header->m_msg_size + sizeof(struct _sw_swop_message_header);
					self->m_read_buffer->m_base = (sw_octets) sw_realloc(self->m_read_buffer->m_base, new_size);
					sw_check(self->m_read_buffer->m_base, exit, err = SW_E_MEM);

               self->m_read_buffer->m_bptr		= self->m_read_buffer->m_base;
               self->m_read_buffer->m_eptr		= self->m_read_buffer->m_base + buflen;
               self->m_read_buffer->m_end		= self->m_read_buffer->m_base + new_size;
               self->m_message->m_header	= (struct _sw_swop_message_header*) self->m_read_buffer->m_base;
            }
            
            self->m_read_buffer->m_bptr += sizeof(struct _sw_swop_message_header);
            buflen -= sizeof(struct _sw_swop_message_header);
         }
      }

      /*
         do we have the whole message?
      */
      if (self->m_message->m_header && (self->m_message->m_header->m_msg_size <= buflen))
      {
			/*
			 * we've read the whole message, so we're now
			 * waiting for a new message
			 */
			self->m_state = Waiting;

			/*
 				side effect endian
 			*/
 			if (endian)
      	{
         	*endian = self->m_message->m_header->m_endian;
 			}

			sw_corby_channel_did_read(self, self->m_read_buffer->m_base, self->m_message->m_header->m_msg_size + SW_SWOP_HEADER_SIZE);

         switch (self->m_message->m_header->m_msg_type)
         {
            case SW_SWOP_REQUEST:

					return sw_corby_channel_parse_request(self, message, op, op_len, buffer);

            case SW_SWOP_REPLY:
      
					return sw_corby_channel_parse_reply(self, message, buffer);

				case SW_SWOP_CANCEL_REQUEST:

					return sw_corby_channel_parse_cancel_request(self, message, buffer);

            case SW_SWOP_LOCATE_REQUEST:

					return sw_corby_channel_parse_locate_request(self, message, buffer);

            case SW_SWOP_LOCATE_REPLY:

					return sw_corby_channel_parse_locate_reply(self, message, buffer);

            case SW_SWOP_CLOSE_CONNECTION:

					return sw_corby_channel_parse_close_connection(self, message, buffer);

            default:
      
               return sw_corby_channel_message_error(self);
         }
      }
      
      /*
         if we get here, then we need to read
      */
      if (block)
      {
			err = sw_socket_recvfrom(self->m_socket, self->m_read_buffer->m_eptr, self->m_read_buffer->m_end - self->m_read_buffer->m_eptr, &len, &self->m_from, &self->m_from_port, NULL, NULL);
			sw_check_okay(err, exit);

			/*
			 * returning 0 is fair because the socket might have been set up
			 * in non-blocking mode.  so if len == 0, we just return from
			 * this function gracefully
			 */
			if (len > 0)
			{
				self->m_read_buffer->m_eptr += len;
				buflen =	(self->m_read_buffer->m_eptr -  self->m_read_buffer->m_bptr);
			}
			else
			{
				break;
			}
      }
		else
		{
			break;
		}
	}

exit:

	return err;
}