blob: 6d533b2f1d2fa0ba9a1e54297628d8b91524596c [file] [log] [blame]
adamdunkels35298692003-08-31 22:16:49 +00001/**
2 * \file
3 * Argument buffer for passing arguments when starting processes
4 * \author Adam Dunkels <adam@dunkels.com>
adamdunkelse937ded2003-10-01 07:53:57 +00005 */
6
7/**
8 * \addtogroup kernel
9 * @{
10 */
11
12/**
13 * \page arg Argument buffer
adamdunkels35298692003-08-31 22:16:49 +000014 *
15 * The argument buffer can be used when passing an argument from an
16 * exiting process to a process that has not been created yet. Since
17 * the exiting process will have exited when the new process is
18 * started, the argument cannot be passed in any of the processes'
19 * addres spaces. In such situations, the argument buffer can be used.
20 *
21 * The argument buffer is statically allocated in memory and is
22 * globally accessible to all processes.
23 *
24 * An argument buffer is allocated with the arg_alloc() function and
25 * deallocated with the arg_free() function. The arg_free() function
26 * is designed so that it can take any pointer, not just an argument
27 * buffer pointer. If the pointer to arg_free() is not an argument
28 * buffer, the function does nothing.
29 */
30
adamdunkels564b9032003-08-27 12:01:46 +000031/*
32 * Copyright (c) 2003, Adam Dunkels.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above
41 * copyright notice, this list of conditions and the following
42 * disclaimer in the documentation and/or other materials provided
43 * with the distribution.
adamdunkels35298692003-08-31 22:16:49 +000044 * 3. The name of the author may not be used to endorse or promote
adamdunkels564b9032003-08-27 12:01:46 +000045 * products derived from this software without specific prior
46 * written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
49 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
52 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
54 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 * This file is part of the Contiki desktop OS
61 *
adamdunkelse937ded2003-10-01 07:53:57 +000062 * $Id: arg.c,v 1.3 2003/10/01 07:53:57 adamdunkels Exp $
adamdunkels564b9032003-08-27 12:01:46 +000063 *
64 */
65
66#include "arg.h"
67
adamdunkels35298692003-08-31 22:16:49 +000068/**
69 * \internal Structure used for holding an argument buffer.
70 */
adamdunkels564b9032003-08-27 12:01:46 +000071struct argbuf {
72 char buf[128];
73 char used;
74};
75
76static struct argbuf bufs[1];
77
78/*-----------------------------------------------------------------------------------*/
adamdunkels35298692003-08-31 22:16:49 +000079/**
80 * \internal Initalizer, called by the dispatcher module.
81 */
82/*-----------------------------------------------------------------------------------*/
adamdunkels564b9032003-08-27 12:01:46 +000083void
84arg_init(void)
85{
86 bufs[0].used = 0;
87}
88/*-----------------------------------------------------------------------------------*/
adamdunkels35298692003-08-31 22:16:49 +000089/**
90 * Allocates an argument buffer.
91 *
92 * \param size The requested size of the buffer, in bytes.
93 *
94 * \return Pointer to allocated buffer, or NULL if no buffer could be
95 * allocated.
96 *
97 * \note It currently is not possible to allocate argument buffers of
98 * any other size than 128 bytes.
99 *
100 */
101/*-----------------------------------------------------------------------------------*/
adamdunkels564b9032003-08-27 12:01:46 +0000102char *
103arg_alloc(char size)
104{
105 if(bufs[0].used == 0) {
106 bufs[0].used = 1;
adamdunkelse937ded2003-10-01 07:53:57 +0000107 return bufs[0].buf;
adamdunkels564b9032003-08-27 12:01:46 +0000108 }
109 return 0;
110}
111/*-----------------------------------------------------------------------------------*/
adamdunkels35298692003-08-31 22:16:49 +0000112/**
113 * Deallocates an argument buffer.
114 *
115 * This function deallocates the argument buffer pointed to by the
116 * parameter, but only if the buffer actually is an argument buffer
117 * and is allocated. It is perfectly safe to call this function with
118 * any pointer.
119 *
120 * \param arg A pointer.
121 */
122/*-----------------------------------------------------------------------------------*/
adamdunkels564b9032003-08-27 12:01:46 +0000123void
124arg_free(char *arg)
125{
126 if(arg == bufs[0].buf) {
127 bufs[0].used = 0;
128 }
129}
130/*-----------------------------------------------------------------------------------*/
adamdunkelse937ded2003-10-01 07:53:57 +0000131/** @} */