Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 998 → Rev 999

/uspace/trunk/libadt/include/list.h
177,7 → 177,7
headless_list_split_or_concat(part1, part2);
}
 
#define list_get_instance(link,type,member) (type *)(((__u8*)(link))-((__u8*)&(((type *)NULL)->member)))
#define list_get_instance(link,type,member) (type *)(((char *)(link))-((char *)&(((type *)NULL)->member)))
 
extern int list_member(const link_t *link, const link_t *head);
extern void list_concat(link_t *head1, link_t *head2);
/uspace/trunk/libipc/include/ipc.h
32,27 → 32,30
#include <kernel/ipc/ipc.h>
#include <libc.h>
 
typedef sysarg_t ipc_data_t[IPC_CALL_LEN];
typedef sysarg_t ipcarg_t;
typedef ipcarg_t ipc_data_t[IPC_CALL_LEN];
typedef sysarg_t ipc_callid_t;
 
typedef void (* ipc_async_callback_t)(ipc_data_t *data);
typedef void (* ipc_async_callback_t)(void *private,
int retval,
ipc_data_t *data);
 
#define ipc_call_sync_2(phoneid, method, arg1, arg2, res1, res2) ipc_call_sync_3((phoneid), (method), (arg1), (arg2), 0, (res1), (res2), 0)
extern int ipc_call_sync_3(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t arg2, sysarg_t arg3,
sysarg_t *result1, sysarg_t *result2,
sysarg_t *result3);
extern int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3,
ipcarg_t *result1, ipcarg_t *result2,
ipcarg_t *result3);
 
 
extern int ipc_call_sync(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t *result);
extern int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t *result);
extern int ipc_wait_for_call(ipc_data_t *data, int flags);
extern void ipc_answer(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
sysarg_t arg2);
extern void ipc_answer(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
ipcarg_t arg2);
 
#define ipc_call_async(phoneid,method,arg1,callback) (ipc_call_async_2(phoneid, method, arg1, 0, callback))
void ipc_call_async_2(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t arg2,
#define ipc_call_async(phoneid,method,arg1,private, callback) (ipc_call_async_2(phoneid, method, arg1, 0, private, callback))
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, void *private,
ipc_async_callback_t callback);
 
#endif
/uspace/trunk/libipc/generic/ipc.c
28,14 → 28,40
 
#include <ipc.h>
#include <libc.h>
#include <malloc.h>
#include <errno.h>
#include <list.h>
#include <stdio.h>
#include <unistd.h>
 
int ipc_call_sync(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t *result)
/** Structure used for keeping track of sent async msgs
* and queing unsent msgs
*
*/
typedef struct {
link_t list;
 
ipc_async_callback_t callback;
void *private;
union {
ipc_callid_t callid;
struct {
int phoneid;
ipc_data_t data;
} msg;
}u;
} async_call_t;
 
LIST_INITIALIZE(dispatched_calls);
LIST_INITIALIZE(queued_calls);
 
int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t *result)
{
ipc_data_t resdata;
int callres;
callres = __SYSCALL4(SYS_IPC_CALL_SYNC, phoneid, method, arg1,
callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
(sysarg_t)&resdata);
if (callres)
return callres;
44,9 → 70,9
return IPC_GET_RETVAL(resdata);
}
 
int ipc_call_sync_3(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t arg2, sysarg_t arg3,
sysarg_t *result1, sysarg_t *result2, sysarg_t *result3)
int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3,
ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3)
{
ipc_data_t data;
int callres;
56,7 → 82,7
IPC_SET_ARG2(data, arg2);
IPC_SET_ARG3(data, arg3);
 
callres = __SYSCALL2(SYS_IPC_CALL_SYNC_MEDIUM, phoneid, (sysarg_t)&data);
callres = __SYSCALL2(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data);
if (callres)
return callres;
 
69,41 → 95,60
return IPC_GET_RETVAL(data);
}
 
/** Syscall to send asynchronous message */
static ipc_callid_t _ipc_call_async(int phoneid, ipc_data_t *data)
{
return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data);
}
 
/** Send asynchronous message
*
* - if fatal error, call callback handler with proper error code
* - if message cannot be temporarily sent, add to queue
*/
void ipc_call_async_2(int phoneid, sysarg_t method, sysarg_t arg1,
sysarg_t arg2,
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, void *private,
ipc_async_callback_t callback)
{
async_call_t *call;
ipc_callid_t callid;
ipc_data_t data; /* Data storage for saving calls */
 
callid = __SYSCALL4(SYS_IPC_CALL_ASYNC, phoneid, method, arg1, arg2);
call = malloc(sizeof(*call));
if (!call) {
callback(private, ENOMEM, NULL);
}
callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
if (callid == IPC_CALLRET_FATAL) {
/* Call asynchronous handler with error code */
IPC_SET_RETVAL(data, IPC_CALLRET_FATAL);
callback(&data);
IPC_SET_RETVAL(call->u.msg.data, ENOENT);
callback(private, ENOENT, NULL);
free(call);
return;
}
 
call->callback = callback;
call->private = private;
 
if (callid == IPC_CALLRET_TEMPORARY) {
/* Add asynchronous call to queue of non-dispatched async calls */
IPC_SET_METHOD(data, method);
IPC_SET_ARG1(data, arg1);
IPC_SET_ARG2(data, arg2);
call->u.msg.phoneid = phoneid;
IPC_SET_METHOD(call->u.msg.data, method);
IPC_SET_ARG1(call->u.msg.data, arg1);
IPC_SET_ARG2(call->u.msg.data, arg2);
 
list_append(&call->list, &queued_calls);
return;
}
/* Add callid to list of dispatched calls */
call->u.callid = callid;
/* Add call to list of dispatched calls */
list_append(&call->list, &dispatched_calls);
}
 
 
/** Send answer to a received call */
void ipc_answer(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
sysarg_t arg2)
void ipc_answer(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
ipcarg_t arg2)
{
__SYSCALL4(SYS_IPC_ANSWER, callid, retval, arg1, arg2);
}
115,9 → 160,65
return __SYSCALL2(SYS_IPC_WAIT, (sysarg_t)data, flags);
}
 
/** Try to dispatch queed calls from async queue */
static void try_dispatch_queued_calls(void)
{
async_call_t *call;
ipc_callid_t callid;
 
while (!list_empty(&queued_calls)) {
call = list_get_instance(queued_calls.next, async_call_t,
list);
 
callid = _ipc_call_async(call->u.msg.phoneid,
&call->u.msg.data);
if (callid == IPC_CALLRET_TEMPORARY)
break;
list_remove(&call->list);
if (callid == IPC_CALLRET_FATAL) {
call->callback(call->private, ENOENT, NULL);
free(call);
} else {
call->u.callid = callid;
list_append(&call->list, &dispatched_calls);
}
}
}
 
/** Handle received answer
*
* TODO: Make it use hash table
*
* @param callid Callid (with first bit set) of the answered call
*/
static void handle_answer(ipc_callid_t callid, ipc_data_t *data)
{
link_t *item;
async_call_t *call;
 
callid &= ~IPC_CALLID_ANSWERED;
for (item = dispatched_calls.next; item != &dispatched_calls;
item = item->next) {
call = list_get_instance(item, async_call_t, list);
if (call->u.callid == callid) {
list_remove(&call->list);
call->callback(call->private,
IPC_GET_RETVAL(*data),
data);
return;
}
}
printf("Received unidentified answer: %P!!!\n", callid);
}
 
 
/** Wait for IPC call and return
*
* - dispatch ASYNC reoutines in the background
* @param data Space where the message is stored
* @return Callid or 0 if nothing available and started with
* IPC_WAIT_NONBLOCKING
*/
int ipc_wait_for_call(ipc_data_t *data, int flags)
{
124,11 → 225,13
ipc_callid_t callid;
 
do {
/* Try to dispatch non-dispatched async calls */
try_dispatch_queued_calls();
 
callid = _ipc_wait_for_call(data, flags);
if (callid & IPC_CALLID_ANSWERED) {
/* TODO: Call async answer handler */
}
/* Handle received answers */
if (callid & IPC_CALLID_ANSWERED)
handle_answer(callid, data);
} while (callid & IPC_CALLID_ANSWERED);
 
return callid;
}
/uspace/trunk/libipc/Makefile
36,7 → 36,7
 
include $(LIBC_PREFIX)/Makefile.toolchain
 
CFLAGS += -Iinclude
CFLAGS += -Iinclude -I../libadt/include -I../libc/include
 
## Sources
#
/uspace/trunk/init/init.c
32,6 → 32,7
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
/*
static void test_printf(void)
{
44,6 → 45,7
printf("Thats all, folks!\n");
}
*/
 
/*
static void test_mremap(void)
{
89,11 → 91,47
}
*/
 
/*
static void got_answer(void *private, int retval, ipc_data_t *data)
{
printf("Retval: %d...%s...%X, %X\n", retval, private,
IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
}
static void test_async_ipc(void)
{
ipc_data_t data;
int i;
 
printf("Sending ping\n");
ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
"Pong1", got_answer);
ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4,
"Pong2", got_answer);
ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4,
"Pong3", got_answer);
ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4,
"Pong4", got_answer);
ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4,
"Pong5", got_answer);
ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4,
"Pong6", got_answer);
printf("Waiting forever...\n");
for (i=0; i<100;i++)
printf(".");
printf("\n");
ipc_wait_for_call(&data, NULL);
printf("Received call???\n");
}
*/
 
int main(int argc, char *argv[])
{
ipcarg_t arg1, arg2;
 
version_print();
 
ipc_call_sync_2(PHONE_NS, NS_PING, 2, 0, 0, 0);
ipc_call_sync_2(PHONE_NS, NS_PING, 0xaaaa, 0xbbbb, &arg1, &arg2);
printf("Pong: %P %P\n", arg1, arg2);
return 0;
}
/uspace/trunk/init/Makefile
58,7 → 58,7
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend
 
$(OUTPUT): $(OBJECTS)
$(LD) -T $(LIBC_PREFIX)/arch/$(ARCH)/_link.ld $(OBJECTS) $(LIBC_PREFIX)/libc.a $(LIBIPC_PREFIX)/libipc.a $(LFLAGS) -o $@ -Map $(OUTPUT).map
$(LD) -T $(LIBC_PREFIX)/arch/$(ARCH)/_link.ld $(OBJECTS) $(LIBIPC_PREFIX)/libipc.a $(LIBC_PREFIX)/libc.a $(LFLAGS) -o $@ -Map $(OUTPUT).map
 
disasm:
$(OBJDUMP) -d $(OUTPUT) >$(OUTPUT).disasm
/uspace/trunk/libc/include/string.h
30,21 → 30,7
#ifndef __LIBC__STRING_H__
#define __LIBC__STRING_H__
 
static inline void * memset(void *s, int c, size_t n)
{
char *os = s;
while (n--)
*(os++) = c;
return s;
}
void * memset(void *s, int c, size_t n);
void * memcpy(void *dest, void *src, size_t n);
 
static inline void * memcpy(void *dest, void *src, size_t n)
{
char *os = src;
char *odst = dest;
while (n--)
*(odst++) = *(os++);
return dest;
}
 
#endif
/uspace/trunk/libc/malloc/malloc.c
438,113 → 438,40
 
*/
 
#ifndef WIN32
#ifdef _WIN32
#define WIN32 1
#endif /* _WIN32 */
#endif /* WIN32 */
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define HAVE_MMAP 1
#define HAVE_MORECORE 0
#define LACKS_UNISTD_H
#include <sys/types.h> /* For size_t */
 
/** Non-default helenos customizations */
#define LACKS_FCNTL_H
#define LACKS_SYS_MMAN_H
#define LACKS_SYS_PARAM_H
#define LACKS_SYS_MMAN_H
#define LACKS_STRING_H
#define LACKS_STRINGS_H
#define LACKS_SYS_TYPES_H
#undef HAVE_MMAP
#define HAVE_MMAP 0
#define LACKS_ERRNO_H
/* Set errno? */
#undef MALLOC_FAILURE_ACTION
#define MALLOC_FAILURE_ACTION
#define MMAP_CLEARS 0 /* WINCE and some others apparently don't clear */
#endif /* WIN32 */
 
#if defined(DARWIN) || defined(_DARWIN)
/* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
#ifndef HAVE_MORECORE
#define HAVE_MORECORE 0
#define HAVE_MMAP 1
#endif /* HAVE_MORECORE */
#endif /* DARWIN */
 
#ifndef LACKS_SYS_TYPES_H
#include <sys/types.h> /* For size_t */
#endif /* LACKS_SYS_TYPES_H */
 
/* The maximum possible size_t value has all bits set */
#define MAX_SIZE_T (~(size_t)0)
 
#ifndef ONLY_MSPACES
#define ONLY_MSPACES 0
#endif /* ONLY_MSPACES */
#ifndef MSPACES
#if ONLY_MSPACES
#define MSPACES 1
#else /* ONLY_MSPACES */
#define MSPACES 0
#endif /* ONLY_MSPACES */
#endif /* MSPACES */
#ifndef MALLOC_ALIGNMENT
#define MALLOC_ALIGNMENT ((size_t)8U)
#endif /* MALLOC_ALIGNMENT */
#ifndef FOOTERS
#define FOOTERS 0
#endif /* FOOTERS */
#ifndef ABORT
#define ABORT abort()
#endif /* ABORT */
#ifndef ABORT_ON_ASSERT_FAILURE
#define ABORT_ON_ASSERT_FAILURE 1
#endif /* ABORT_ON_ASSERT_FAILURE */
#ifndef PROCEED_ON_ERROR
#define PROCEED_ON_ERROR 0
#endif /* PROCEED_ON_ERROR */
#ifndef USE_LOCKS
#define USE_LOCKS 0
#endif /* USE_LOCKS */
#ifndef INSECURE
#define INSECURE 0
#endif /* INSECURE */
#ifndef HAVE_MMAP
#define HAVE_MMAP 1
#endif /* HAVE_MMAP */
#ifndef MMAP_CLEARS
#define HAVE_MMAP 0
 
#define MMAP_CLEARS 1
#endif /* MMAP_CLEARS */
#ifndef HAVE_MREMAP
#ifdef linux
#define HAVE_MREMAP 1
#else /* linux */
#define HAVE_MREMAP 0
#endif /* linux */
#endif /* HAVE_MREMAP */
#ifndef MALLOC_FAILURE_ACTION
#define MALLOC_FAILURE_ACTION errno = ENOMEM;
#endif /* MALLOC_FAILURE_ACTION */
#ifndef HAVE_MORECORE
#if ONLY_MSPACES
#define HAVE_MORECORE 0
#else /* ONLY_MSPACES */
 
#define HAVE_MORECORE 1
#endif /* ONLY_MSPACES */
#endif /* HAVE_MORECORE */
#if !HAVE_MORECORE
#define MORECORE_CONTIGUOUS 0
#else /* !HAVE_MORECORE */
#ifndef MORECORE
#define MORECORE_CONTIGUOUS 1
#define MORECORE sbrk
#endif /* MORECORE */
#ifndef MORECORE_CONTIGUOUS
#define MORECORE_CONTIGUOUS 1
#endif /* MORECORE_CONTIGUOUS */
#endif /* HAVE_MORECORE */
#ifndef DEFAULT_GRANULARITY
#if MORECORE_CONTIGUOUS
#define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */
#else /* MORECORE_CONTIGUOUS */
#define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
#endif /* MORECORE_CONTIGUOUS */
#endif /* DEFAULT_GRANULARITY */
 
#ifndef DEFAULT_TRIM_THRESHOLD
#ifndef MORECORE_CANNOT_TRIM
#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
583,18 → 510,6
#define M_GRANULARITY (-2)
#define M_MMAP_THRESHOLD (-3)
 
/** Non-default helenos customizations */
#define LACKS_FCNTL_H
#define LACKS_SYS_MMAN_H
#define LACKS_SYS_PARAM_H
#undef HAVE_MMAP
#define HAVE_MMAP 0
#define LACKS_ERRNO_H
/* Set errno? */
#undef MALLOC_FAILURE_ACTION
#define MALLOC_FAILURE_ACTION
 
 
/*
========================================================================
To make a fully customizable malloc.h header file, cut everything
607,11 → 522,8
 
/*------------------------------ internal #includes ---------------------- */
 
#ifdef WIN32
#pragma warning( disable : 4146 ) /* no "unsigned" warnings */
#endif /* WIN32 */
 
#include <stdio.h> /* for printing in malloc_stats */
#include <string.h>
 
#ifndef LACKS_ERRNO_H
#include <errno.h> /* for MALLOC_FAILURE_ACTION */
631,9 → 543,6
#else /* DEBUG */
#define assert(x)
#endif /* DEBUG */
#ifndef LACKS_STRING_H
#include <string.h> /* for memset etc */
#endif /* LACKS_STRING_H */
#if USE_BUILTIN_FFS
#ifndef LACKS_STRINGS_H
#include <strings.h> /* for ffs */
/uspace/trunk/libc/generic/string.c
0,0 → 1,49
/*
* Copyright (C) 2005 Martin Decky
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
#include <types.h>
#include <string.h>
 
/* Dummy implementation of mem/ functions */
 
void * memset(void *s, int c, size_t n)
{
char *os = s;
while (n--)
*(os++) = c;
return s;
}
 
void * memcpy(void *dest, void *src, size_t n)
{
char *os = src;
char *odst = dest;
while (n--)
*(odst++) = *(os++);
return dest;
}
/uspace/trunk/libc/generic/mmap.c
62,7 → 62,11
res = mremap(&_heap, heapsize + incr,0);
if (!res)
return NULL;
res = (void *)&_heap + incr;
/* Compute start of new area */
res = (void *)&_heap + heapsize;
 
heapsize += incr;
 
return res;
}
/uspace/trunk/libc/Makefile
42,6 → 42,7
GENERIC_SOURCES = \
generic/libc.c \
generic/mmap.c \
generic/string.c \
generic/io/io.c \
generic/io/print.c \
malloc/malloc.c