Contiki 2.6

arg.c

Go to the documentation of this file.
00001 /**
00002  * \file
00003  * Argument buffer for passing arguments when starting processes
00004  * \author Adam Dunkels <adam@dunkels.com>
00005  */
00006 
00007 /**
00008  * \addtogroup sys
00009  * @{
00010  */
00011 
00012 /**
00013  * \defgroup arg Argument buffer
00014  * @{
00015  *
00016  * The argument buffer can be used when passing an argument from an
00017  * exiting process to a process that has not been created yet. Since
00018  * the exiting process will have exited when the new process is
00019  * started, the argument cannot be passed in any of the processes'
00020  * addres spaces. In such situations, the argument buffer can be used.
00021  *
00022  * The argument buffer is statically allocated in memory and is
00023  * globally accessible to all processes.
00024  *
00025  * An argument buffer is allocated with the arg_alloc() function and
00026  * deallocated with the arg_free() function. The arg_free() function
00027  * is designed so that it can take any pointer, not just an argument
00028  * buffer pointer. If the pointer to arg_free() is not an argument
00029  * buffer, the function does nothing.
00030  */
00031 
00032 /*
00033  * Copyright (c) 2003, Adam Dunkels.
00034  * All rights reserved.
00035  *
00036  * Redistribution and use in source and binary forms, with or without
00037  * modification, are permitted provided that the following conditions
00038  * are met:
00039  * 1. Redistributions of source code must retain the above copyright
00040  *    notice, this list of conditions and the following disclaimer.
00041  * 2. Redistributions in binary form must reproduce the above
00042  *    copyright notice, this list of conditions and the following
00043  *    disclaimer in the documentation and/or other materials provided
00044  *    with the distribution.
00045  * 3. The name of the author may not be used to endorse or promote
00046  *    products derived from this software without specific prior
00047  *    written permission.
00048  *
00049  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00050  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00051  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00052  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00053  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00054  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00055  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00056  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00057  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00058  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00059  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00060  *
00061  * This file is part of the Contiki desktop OS
00062  *
00063  * $Id: arg.c,v 1.2 2006/08/14 23:39:23 oliverschmidt Exp $
00064  *
00065  */
00066 
00067 #include "contiki.h"
00068 #include "sys/arg.h"
00069 
00070 /**
00071  * \internal Structure used for holding an argument buffer.
00072  */
00073 struct argbuf {
00074   char buf[128];
00075   char used;
00076 };
00077 
00078 static struct argbuf bufs[1];
00079 
00080 /*-----------------------------------------------------------------------------------*/
00081 /**
00082  * \internal Initalizer, called by the dispatcher module.
00083  */
00084 /*-----------------------------------------------------------------------------------*/
00085 void
00086 arg_init(void)
00087 {
00088   bufs[0].used = 0;
00089 }
00090 /*-----------------------------------------------------------------------------------*/
00091 /**
00092  * Allocates an argument buffer.
00093  *
00094  * \param size The requested size of the buffer, in bytes.
00095  *
00096  * \return Pointer to allocated buffer, or NULL if no buffer could be
00097  * allocated.
00098  *
00099  * \note It currently is not possible to allocate argument buffers of
00100  * any other size than 128 bytes.
00101  *
00102  */
00103 /*-----------------------------------------------------------------------------------*/
00104 char *
00105 arg_alloc(char size)
00106 {
00107   if(bufs[0].used == 0) {
00108     bufs[0].used = 1;
00109     return bufs[0].buf;
00110   }
00111   return 0;
00112 }
00113 /*-----------------------------------------------------------------------------------*/
00114 /**
00115  * Deallocates an argument buffer.
00116  *
00117  * This function deallocates the argument buffer pointed to by the
00118  * parameter, but only if the buffer actually is an argument buffer
00119  * and is allocated. It is perfectly safe to call this function with
00120  * any pointer.
00121  *
00122  * \param arg A pointer.
00123  */
00124 /*-----------------------------------------------------------------------------------*/
00125 void
00126 arg_free(char *arg)
00127 {
00128   if(arg == bufs[0].buf) {
00129     bufs[0].used = 0;
00130   }
00131 }
00132 /*-----------------------------------------------------------------------------------*/
00133 /** @} */
00134 /** @} */