Files
nuttx/sched/mqueue/mq_msgqalloc.c
T
anjiahao b8eb7e36ed mq_msgqalloc:del check MQ_MAX_MSGS
N/A

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Change-Id: If43fdd937f23de24921f5a14072fa37d9d339c94
2021-07-15 20:32:03 +08:00

107 lines
3.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/****************************************************************************
* sched/mqueue/mq_msgqalloc.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <mqueue.h>
#include <assert.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h>
#include "sched/sched.h"
#include "mqueue/mqueue.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxmq_alloc_msgq
*
* Description:
* This function implements a part of the POSIX message queue open logic.
* It allocates and initializes a struct mqueue_inode_s structure.
*
* Input Parameters:
* attr - The mq_maxmsg attribute is used at the time that the message
* queue is created to determine the maximum number of
* messages that may be placed in the message queue.
* msgq - This parameter is a address of a pointer
*
* Returned Value:
* IF the function runs successfullywill return Zero(OK),else will
* return a error code.
*
* EINVAL attr is a null pointer and attr->mq_mqssize or attr->mq_maxmsg
* is an invalid value.
* ENOMEM No memery to alloc.
*
****************************************************************************/
int nxmq_alloc_msgq(FAR struct mq_attr *attr,
FAR struct mqueue_inode_s **msgq)
{
/* Check if the caller is attempting to allocate a message for messages
* larger than the configured maximum message size.
*/
DEBUGASSERT((!attr || attr->mq_msgsize <= MQ_MAX_BYTES) && msgq);
if ((attr && attr->mq_msgsize > MQ_MAX_BYTES) || !msgq)
{
return -EINVAL;
}
/* Allocate memory for the new message queue. */
*msgq = (FAR struct mqueue_inode_s *)
kmm_zalloc(sizeof(struct mqueue_inode_s));
if (*msgq)
{
/* Initialize the new named message queue */
sq_init(&(*msgq)->msglist);
if (attr)
{
(*msgq)->maxmsgs = (int16_t)attr->mq_maxmsg;
(*msgq)->maxmsgsize = (int16_t)attr->mq_msgsize;
}
else
{
(*msgq)->maxmsgs = MQ_MAX_MSGS;
(*msgq)->maxmsgsize = MQ_MAX_BYTES;
}
(*msgq)->ntpid = INVALID_PROCESS_ID;
}
else
{
return -ENOMEM;
}
return OK;
}