/branches/network/uspace/lib/libc/generic/kbd.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/libadt/hash_table.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/libadt/list.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/console.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/ddi.c |
---|
127,10 → 127,10 |
} |
#endif |
phys = ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE); |
phys = (void *) ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE); |
offset = pio_addr - phys; |
pages = ALIGN_UP(offset + size, PAGE_SIZE) >> PAGE_WIDTH; |
virt = as_get_mappable_page(pages); |
virt = as_get_mappable_page(pages << PAGE_WIDTH); |
*use_addr = virt + offset; |
return physmem_map(phys, virt, pages, AS_AREA_READ | AS_AREA_WRITE); |
} |
/branches/network/uspace/lib/libc/generic/task.c |
---|
39,11 → 39,13 |
#include <errno.h> |
#include <loader/loader.h> |
#include <string.h> |
#include <ipc/ns.h> |
#include <macros.h> |
#include <async.h> |
task_id_t task_get_id(void) |
{ |
task_id_t task_id; |
(void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id); |
return task_id; |
53,7 → 55,9 |
* |
* @param name The new name, typically the command used to execute the |
* program. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int task_set_name(const char *name) |
{ |
67,21 → 71,20 |
* |
* @param path pathname of the binary to execute |
* @param argv command-line arguments |
* |
* @return ID of the newly created task or zero on error. |
* |
*/ |
task_id_t task_spawn(const char *path, char *const argv[]) |
task_id_t task_spawn(const char *path, char *const args[]) |
{ |
loader_t *ldr; |
task_id_t task_id; |
int rc; |
/* Connect to a program loader. */ |
ldr = loader_connect(); |
loader_t *ldr = loader_connect(); |
if (ldr == NULL) |
return 0; |
/* Get task ID. */ |
rc = loader_get_task_id(ldr, &task_id); |
task_id_t task_id; |
int rc = loader_get_task_id(ldr, &task_id); |
if (rc != EOK) |
goto error; |
91,10 → 94,38 |
goto error; |
/* Send arguments. */ |
rc = loader_set_args(ldr, argv); |
rc = loader_set_args(ldr, args); |
if (rc != EOK) |
goto error; |
/* Send default files */ |
fdi_node_t *files[4]; |
fdi_node_t stdin_node; |
fdi_node_t stdout_node; |
fdi_node_t stderr_node; |
if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK)) |
files[0] = &stdin_node; |
else |
files[0] = NULL; |
if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK)) |
files[1] = &stdout_node; |
else |
files[1] = NULL; |
if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK)) |
files[2] = &stderr_node; |
else |
files[2] = NULL; |
files[3] = NULL; |
rc = loader_set_files(ldr, files); |
if (rc != EOK) |
goto error; |
/* Load the program. */ |
rc = loader_load_program(ldr); |
if (rc != EOK) |
106,12 → 137,11 |
goto error; |
/* Success */ |
free(ldr); |
return task_id; |
error: |
/* Error exit */ |
error: |
loader_abort(ldr); |
free(ldr); |
118,5 → 148,10 |
return 0; |
} |
int task_wait(task_id_t id) |
{ |
return (int) async_req_2_0(PHONE_NS, NS_TASK_WAIT, LOWER32(id), UPPER32(id)); |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/fibril_sync.c |
---|
0,0 → 1,219 |
/* |
* Copyright (c) 2009 Jakub Jermar |
* 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. |
*/ |
/** @addtogroup libc |
* @{ |
*/ |
/** @file |
*/ |
#include <fibril_sync.h> |
#include <fibril.h> |
#include <async.h> |
#include <adt/list.h> |
#include <futex.h> |
#include <assert.h> |
void fibril_mutex_initialize(fibril_mutex_t *fm) |
{ |
fm->counter = 1; |
list_initialize(&fm->waiters); |
} |
void fibril_mutex_lock(fibril_mutex_t *fm) |
{ |
futex_down(&async_futex); |
if (fm->counter-- <= 0) { |
fibril_t *f = (fibril_t *) fibril_get_id(); |
list_append(&f->link, &fm->waiters); |
fibril_switch(FIBRIL_TO_MANAGER); |
} else { |
futex_up(&async_futex); |
} |
} |
bool fibril_mutex_trylock(fibril_mutex_t *fm) |
{ |
bool locked = false; |
futex_down(&async_futex); |
if (fm->counter > 0) { |
fm->counter--; |
locked = true; |
} |
futex_up(&async_futex); |
return locked; |
} |
static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm) |
{ |
assert(fm->counter <= 0); |
if (fm->counter++ < 0) { |
link_t *tmp; |
fibril_t *f; |
assert(!list_empty(&fm->waiters)); |
tmp = fm->waiters.next; |
f = list_get_instance(tmp, fibril_t, link); |
list_remove(&f->link); |
fibril_add_ready((fid_t) f); |
} |
} |
void fibril_mutex_unlock(fibril_mutex_t *fm) |
{ |
futex_down(&async_futex); |
_fibril_mutex_unlock_unsafe(fm); |
futex_up(&async_futex); |
} |
void fibril_rwlock_initialize(fibril_rwlock_t *frw) |
{ |
frw->writers = 0; |
frw->readers = 0; |
list_initialize(&frw->waiters); |
} |
void fibril_rwlock_read_lock(fibril_rwlock_t *frw) |
{ |
futex_down(&async_futex); |
if (frw->writers) { |
fibril_t *f = (fibril_t *) fibril_get_id(); |
f->flags &= ~FIBRIL_WRITER; |
list_append(&f->link, &frw->waiters); |
fibril_switch(FIBRIL_TO_MANAGER); |
} else { |
frw->readers++; |
futex_up(&async_futex); |
} |
} |
void fibril_rwlock_write_lock(fibril_rwlock_t *frw) |
{ |
futex_down(&async_futex); |
if (frw->writers || frw->readers) { |
fibril_t *f = (fibril_t *) fibril_get_id(); |
f->flags |= FIBRIL_WRITER; |
list_append(&f->link, &frw->waiters); |
fibril_switch(FIBRIL_TO_MANAGER); |
} else { |
frw->writers++; |
futex_up(&async_futex); |
} |
} |
static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw) |
{ |
futex_down(&async_futex); |
assert(frw->readers || (frw->writers == 1)); |
if (frw->readers) { |
if (--frw->readers) |
goto out; |
} else { |
frw->writers--; |
} |
assert(!frw->readers && !frw->writers); |
while (!list_empty(&frw->waiters)) { |
link_t *tmp = frw->waiters.next; |
fibril_t *f = list_get_instance(tmp, fibril_t, link); |
if (f->flags & FIBRIL_WRITER) { |
if (frw->readers) |
break; |
list_remove(&f->link); |
fibril_add_ready((fid_t) f); |
frw->writers++; |
break; |
} else { |
list_remove(&f->link); |
fibril_add_ready((fid_t) f); |
frw->readers++; |
} |
} |
out: |
futex_up(&async_futex); |
} |
void fibril_rwlock_read_unlock(fibril_rwlock_t *frw) |
{ |
_fibril_rwlock_common_unlock(frw); |
} |
void fibril_rwlock_write_unlock(fibril_rwlock_t *frw) |
{ |
_fibril_rwlock_common_unlock(frw); |
} |
void fibril_condvar_initialize(fibril_condvar_t *fcv) |
{ |
list_initialize(&fcv->waiters); |
} |
void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm) |
{ |
fibril_t *f = (fibril_t *) fibril_get_id(); |
futex_down(&async_futex); |
list_append(&f->link, &fcv->waiters); |
_fibril_mutex_unlock_unsafe(fm); |
fibril_switch(FIBRIL_TO_MANAGER); |
fibril_mutex_lock(fm); |
} |
static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once) |
{ |
link_t *tmp; |
fibril_t *f; |
futex_down(&async_futex); |
while (!list_empty(&fcv->waiters)) { |
tmp = fcv->waiters.next; |
f = list_get_instance(tmp, fibril_t, link); |
list_remove(&f->link); |
fibril_add_ready((fid_t) f); |
if (once) |
break; |
} |
futex_up(&async_futex); |
} |
void fibril_condvar_signal(fibril_condvar_t *fcv) |
{ |
_fibril_condvar_wakeup_common(fcv, true); |
} |
void fibril_condvar_broadcast(fibril_condvar_t *fcv) |
{ |
_fibril_condvar_wakeup_common(fcv, false); |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/as.c |
---|
119,15 → 119,15 |
void *res; |
/* Check for invalid values */ |
if (incr < 0 && -incr > heapsize) |
if ((incr < 0) && (((size_t) -incr) > heapsize)) |
return NULL; |
/* Check for too large value */ |
if (incr > 0 && incr+heapsize < heapsize) |
if ((incr > 0) && (incr + heapsize < heapsize)) |
return NULL; |
/* Check for too small values */ |
if (incr < 0 && incr+heapsize > heapsize) |
if ((incr < 0) && (incr + heapsize > heapsize)) |
return NULL; |
/* Check for user limit */ |
175,7 → 175,7 |
asz = 1 << (fnzb64(sz - 1) + 1); |
/* Set heapsize to some meaningful value */ |
if (maxheapsize == -1) |
if (maxheapsize == (size_t) -1) |
set_maxheapsize(MAX_HEAP_SIZE); |
/* |
/branches/network/uspace/lib/libc/generic/string.c |
---|
243,9 → 243,9 |
* @return Number of bytes used by the characters. |
* |
*/ |
size_t str_lsize(const char *str, count_t max_len) |
size_t str_lsize(const char *str, size_t max_len) |
{ |
count_t len = 0; |
size_t len = 0; |
size_t offset = 0; |
while (len < max_len) { |
271,7 → 271,7 |
* @return Number of bytes used by the wide characters. |
* |
*/ |
size_t wstr_lsize(const wchar_t *str, count_t max_len) |
size_t wstr_lsize(const wchar_t *str, size_t max_len) |
{ |
return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t)); |
} |
283,9 → 283,9 |
* @return Number of characters in string. |
* |
*/ |
count_t str_length(const char *str) |
size_t str_length(const char *str) |
{ |
count_t len = 0; |
size_t len = 0; |
size_t offset = 0; |
while (str_decode(str, &offset, STR_NO_LIMIT) != 0) |
301,9 → 301,9 |
* @return Number of characters in @a str. |
* |
*/ |
count_t wstr_length(const wchar_t *wstr) |
size_t wstr_length(const wchar_t *wstr) |
{ |
count_t len = 0; |
size_t len = 0; |
while (*wstr++ != 0) |
len++; |
319,9 → 319,9 |
* @return Number of characters in string. |
* |
*/ |
count_t str_nlength(const char *str, size_t size) |
size_t str_nlength(const char *str, size_t size) |
{ |
count_t len = 0; |
size_t len = 0; |
size_t offset = 0; |
while (str_decode(str, &offset, size) != 0) |
338,11 → 338,11 |
* @return Number of characters in string. |
* |
*/ |
count_t wstr_nlength(const wchar_t *str, size_t size) |
size_t wstr_nlength(const wchar_t *str, size_t size) |
{ |
count_t len = 0; |
count_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); |
count_t offset = 0; |
size_t len = 0; |
size_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); |
size_t offset = 0; |
while ((offset < limit) && (*str++ != 0)) { |
len++; |
430,7 → 430,7 |
* 1 if second smaller. |
* |
*/ |
int str_lcmp(const char *s1, const char *s2, count_t max_len) |
int str_lcmp(const char *s1, const char *s2, size_t max_len) |
{ |
wchar_t c1 = 0; |
wchar_t c2 = 0; |
438,7 → 438,7 |
size_t off1 = 0; |
size_t off2 = 0; |
count_t len = 0; |
size_t len = 0; |
while (true) { |
if (len >= max_len) |
568,7 → 568,7 |
return; |
wchar_t ch; |
count_t src_idx = 0; |
size_t src_idx = 0; |
size_t dst_off = 0; |
while ((ch = src[src_idx++]) != 0) { |
616,7 → 616,7 |
wchar_t acc; |
size_t off = 0; |
size_t last = 0; |
char *res = NULL; |
const char *res = NULL; |
while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { |
if (acc == ch) |
641,14 → 641,14 |
* is out of bounds. |
* |
*/ |
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos) |
bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos) |
{ |
count_t len = wstr_length(str); |
size_t len = wstr_length(str); |
if ((pos > len) || (pos + 1 > max_pos)) |
return false; |
count_t i; |
size_t i; |
for (i = len; i + 1 > pos; i--) |
str[i + 1] = str[i]; |
669,14 → 669,14 |
* is out of bounds. |
* |
*/ |
bool wstr_remove(wchar_t *str, count_t pos) |
bool wstr_remove(wchar_t *str, size_t pos) |
{ |
count_t len = wstr_length(str); |
size_t len = wstr_length(str); |
if (pos >= len) |
return false; |
count_t i; |
size_t i; |
for (i = pos + 1; i <= len; i++) |
str[i - 1] = str[i]; |
/branches/network/uspace/lib/libc/generic/loader.c |
---|
47,9 → 47,12 |
/** Connect to a new program loader. |
* |
* Spawns a new program loader task and returns the connection structure. |
* |
* @param name Symbolic name to set on the newly created task. |
* |
* @return Pointer to the loader connection structure (should be |
* de-allocated using free() after use). |
* deallocated using free() after use). |
* |
*/ |
int loader_spawn(const char *name) |
{ |
59,14 → 62,11 |
loader_t *loader_connect(void) |
{ |
loader_t *ldr; |
int phone_id; |
phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0); |
int phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0); |
if (phone_id < 0) |
return NULL; |
ldr = malloc(sizeof(loader_t)); |
loader_t *ldr = malloc(sizeof(loader_t)); |
if (ldr == NULL) |
return NULL; |
80,23 → 80,22 |
* |
* @param ldr Loader connection structure. |
* @param task_id Points to a variable where the ID should be stored. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_get_task_id(loader_t *ldr, task_id_t *task_id) |
{ |
/* Get task ID. */ |
ipc_call_t answer; |
aid_t req; |
int rc; |
ipcarg_t retval; |
/* Get task ID. */ |
req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer); |
rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t)); |
aid_t req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer); |
int rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return rc; |
} |
ipcarg_t retval; |
async_wait_for(req, &retval); |
return (int)retval; |
} |
109,25 → 108,21 |
* |
* @param ldr Loader connection structure. |
* @param path Pathname of the program file. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_set_pathname(loader_t *ldr, const char *path) |
{ |
ipc_call_t answer; |
aid_t req; |
int rc; |
ipcarg_t retval; |
char *pa; |
size_t pa_len; |
pa = absolutize(path, &pa_len); |
char *pa = absolutize(path, &pa_len); |
if (!pa) |
return 0; |
/* Send program pathname */ |
req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer); |
rc = ipc_data_write_start(ldr->phone_id, (void *)pa, pa_len); |
ipc_call_t answer; |
aid_t req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer); |
int rc = ipc_data_write_start(ldr->phone_id, (void *) pa, pa_len); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return rc; |
135,11 → 130,11 |
free(pa); |
ipcarg_t retval; |
async_wait_for(req, &retval); |
return (int)retval; |
} |
/** Set command-line arguments for the program. |
* |
* Sets the vector of command-line arguments to be passed to the loaded |
148,48 → 143,41 |
* |
* @param ldr Loader connection structure. |
* @param argv NULL-terminated array of pointers to arguments. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_set_args(loader_t *ldr, char *const argv[]) |
{ |
aid_t req; |
ipc_call_t answer; |
ipcarg_t rc; |
char *const *ap; |
char *dp; |
char *arg_buf; |
size_t buffer_size; |
/* |
* Serialize the arguments into a single array. First |
* compute size of the buffer needed. |
*/ |
ap = argv; |
buffer_size = 0; |
char *const *ap = argv; |
size_t buffer_size = 0; |
while (*ap != NULL) { |
buffer_size += str_size(*ap) + 1; |
++ap; |
ap++; |
} |
arg_buf = malloc(buffer_size); |
if (arg_buf == NULL) return ENOMEM; |
char *arg_buf = malloc(buffer_size); |
if (arg_buf == NULL) |
return ENOMEM; |
/* Now fill the buffer with null-terminated argument strings */ |
ap = argv; |
dp = arg_buf; |
char *dp = arg_buf; |
while (*ap != NULL) { |
str_cpy(dp, buffer_size - (dp - arg_buf), *ap); |
dp += str_size(*ap) + 1; |
++ap; |
ap++; |
} |
/* Send serialized arguments to the loader */ |
req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer); |
rc = ipc_data_write_start(ldr->phone_id, (void *)arg_buf, buffer_size); |
ipc_call_t answer; |
aid_t req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer); |
ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) arg_buf, buffer_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return rc; |
196,7 → 184,8 |
} |
async_wait_for(req, &rc); |
if (rc != EOK) return rc; |
if (rc != EOK) |
return rc; |
/* Free temporary buffer */ |
free(arg_buf); |
204,25 → 193,76 |
return EOK; |
} |
/** Instruct loader to load the program. |
/** Set preset files for the program. |
* |
* If this function succeeds, the program has been successfully loaded |
* and is ready to be executed. |
* Sets the vector of preset files to be passed to the loaded |
* program. By convention, the first three files represent stdin, |
* stdout and stderr respectively. |
* |
* @param ldr Loader connection structure. |
* @param files NULL-terminated array of pointers to files. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_load_program(loader_t *ldr) |
int loader_set_files(loader_t *ldr, fdi_node_t *const files[]) |
{ |
int rc; |
/* |
* Serialize the arguments into a single array. First |
* compute size of the buffer needed. |
*/ |
fdi_node_t *const *ap = files; |
size_t count = 0; |
while (*ap != NULL) { |
count++; |
ap++; |
} |
rc = async_req_0_0(ldr->phone_id, LOADER_LOAD); |
fdi_node_t *files_buf; |
files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t)); |
if (files_buf == NULL) |
return ENOMEM; |
/* Fill the buffer */ |
size_t i; |
for (i = 0; i < count; i++) |
files_buf[i] = *files[i]; |
/* Send serialized files to the loader */ |
ipc_call_t answer; |
aid_t req = async_send_0(ldr->phone_id, LOADER_SET_FILES, &answer); |
ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) files_buf, |
count * sizeof(fdi_node_t)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return rc; |
} |
async_wait_for(req, &rc); |
if (rc != EOK) |
return rc; |
/* Free temporary buffer */ |
free(files_buf); |
return EOK; |
} |
/** Instruct loader to load the program. |
* |
* If this function succeeds, the program has been successfully loaded |
* and is ready to be executed. |
* |
* @param ldr Loader connection structure. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_load_program(loader_t *ldr) |
{ |
return (int) async_req_0_0(ldr->phone_id, LOADER_LOAD); |
} |
/** Instruct loader to execute the program. |
* |
* Note that this function blocks until the loader actually replies |
233,13 → 273,13 |
* on the loader structure. It should be de-allocated using free(). |
* |
* @param ldr Loader connection structure. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
int loader_run(loader_t *ldr) |
{ |
int rc; |
rc = async_req_0_0(ldr->phone_id, LOADER_RUN); |
int rc = async_req_0_0(ldr->phone_id, LOADER_RUN); |
if (rc != EOK) |
return rc; |
255,7 → 295,9 |
* on the loader structure. It should be de-allocated using free(). |
* |
* @param ldr Loader connection structure. |
* |
* @return Zero on success or negative error code. |
* |
*/ |
void loader_abort(loader_t *ldr) |
{ |
/branches/network/uspace/lib/libc/generic/fibril.c |
---|
33,7 → 33,7 |
/** @file |
*/ |
#include <libadt/list.h> |
#include <adt/list.h> |
#include <fibril.h> |
#include <thread.h> |
#include <tls.h> |
49,7 → 49,8 |
#define FIBRIL_INITIAL_STACK_PAGES_NO 1 |
#endif |
/** This futex serializes access to ready_list, serialized_list and manage_list. |
/** |
* This futex serializes access to ready_list, serialized_list and manager_list. |
*/ |
static atomic_t fibril_futex = FUTEX_INITIALIZER; |
59,12 → 60,12 |
static void fibril_main(void); |
/** Number of fibrils that are in async_serialized mode */ |
static int serialized_fibrils; /* Protected by async_futex */ |
/** Thread-local count of serialization. If >0, we must not preempt */ |
static __thread int serialization_count; |
/** Counter for fibrils residing in async_manager */ |
static int fibrils_in_manager; |
/** Number of threads that are executing a manager fibril. */ |
static int threads_in_manager; |
/** Number of threads that are executing a manager fibril and are serialized. */ |
static int serialized_threads; /* Protected by async_futex */ |
/** Fibril-local count of serialization. If > 0, we must not preempt */ |
static fibril_local int serialization_count; |
/** Setup fibril information into TCB structure */ |
fibril_t *fibril_setup(void) |
143,11 → 144,11 |
if (list_empty(&ready_list) && list_empty(&serialized_list)) |
goto ret_0; |
/* |
* Do not preempt if there is not sufficient count of fibril |
* managers. |
* Do not preempt if there is not enough threads to run the |
* ready fibrils which are not serialized. |
*/ |
if (list_empty(&serialized_list) && |
fibrils_in_manager <= serialized_fibrils) { |
threads_in_manager <= serialized_threads) { |
goto ret_0; |
} |
} |
194,7 → 195,7 |
list_append(&srcf->link, &ready_list); |
else if (stype == FIBRIL_FROM_MANAGER) { |
list_append(&srcf->link, &manager_list); |
fibrils_in_manager--; |
threads_in_manager--; |
} else { |
/* |
* If stype == FIBRIL_TO_MANAGER, don't put ourselves to |
208,10 → 209,10 |
if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) { |
dstf = list_get_instance(manager_list.next, fibril_t, link); |
if (serialization_count && stype == FIBRIL_TO_MANAGER) { |
serialized_fibrils++; |
serialized_threads++; |
srcf->flags |= FIBRIL_SERIALIZED; |
} |
fibrils_in_manager++; |
threads_in_manager++; |
if (stype == FIBRIL_FROM_DEAD) |
dstf->clean_after_me = srcf; |
219,7 → 220,7 |
if (!list_empty(&serialized_list)) { |
dstf = list_get_instance(serialized_list.next, fibril_t, |
link); |
serialized_fibrils--; |
serialized_threads--; |
} else { |
dstf = list_get_instance(ready_list.next, fibril_t, |
link); |
269,7 → 270,7 |
/** Add a fibril to the ready list. |
* |
* @param fid Pinter to the fibril structure of the fibril to be |
* @param fid Pointer to the fibril structure of the fibril to be |
* added. |
*/ |
void fibril_add_ready(fid_t fid) |
287,7 → 288,8 |
/** Add a fibril to the manager list. |
* |
* @param fid Pinter to the fibril structure of the fibril to be added. |
* @param fid Pointer to the fibril structure of the fibril to be |
* added. |
*/ |
void fibril_add_manager(fid_t fid) |
{ |
314,7 → 316,8 |
/** Return fibril id of the currently running fibril. |
* |
* @return Fibril ID of the currently running pseudo thread. |
* @return fibril ID of the currently running fibril. |
* |
*/ |
fid_t fibril_get_id(void) |
{ |
327,7 → 330,8 |
* preempted, it should start async_serialize_start() in the beginning of |
* communication and async_serialize_end() in the end. If it is a true |
* multithreaded application, it should protect the communication channel by a |
* futex as well. Interrupt messages can still be preempted. |
* futex as well. |
* |
*/ |
void fibril_inc_sercount(void) |
{ |
/branches/network/uspace/lib/libc/generic/libc.c |
---|
31,24 → 31,25 |
* @{ |
* @} |
*/ |
/** @addtogroup libc generic |
* @ingroup lc |
* @{ |
*/ |
/** @file |
*/ |
#include <libc.h> |
#include <stdio.h> |
#include <unistd.h> |
#include <malloc.h> |
#include <tls.h> |
#include <thread.h> |
#include <fibril.h> |
#include <io/stream.h> |
#include <ipc/ipc.h> |
#include <async.h> |
#include <as.h> |
#include <console.h> |
#include <loader/pcb.h> |
extern char _heap; |
63,28 → 64,30 |
void __main(void *pcb_ptr) |
{ |
fibril_t *f; |
int argc; |
char **argv; |
(void) as_area_create(&_heap, 1, AS_AREA_WRITE | AS_AREA_READ); |
(void) as_area_create(&_heap, 1, AS_AREA_WRITE | AS_AREA_READ); |
_async_init(); |
f = fibril_setup(); |
__tcb_set(f->tcb); |
fibril_t *fibril = fibril_setup(); |
__tcb_set(fibril->tcb); |
/* Save the PCB pointer */ |
__pcb = (pcb_t *)pcb_ptr; |
int argc; |
char **argv; |
if (__pcb == NULL) { |
argc = 0; |
argv = NULL; |
stdio_init(0, NULL); |
} else { |
argc = __pcb->argc; |
argv = __pcb->argv; |
stdio_init(__pcb->filc, __pcb->filv); |
} |
main(argc, argv); |
console_flush(); |
stdio_done(); |
} |
void __exit(void) |
/branches/network/uspace/lib/libc/generic/ipc.c |
---|
43,7 → 43,7 |
#include <libc.h> |
#include <malloc.h> |
#include <errno.h> |
#include <libadt/list.h> |
#include <adt/list.h> |
#include <stdio.h> |
#include <unistd.h> |
#include <futex.h> |
232,7 → 232,7 |
return; |
} |
if (callid == IPC_CALLRET_FATAL) { |
if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) { |
futex_up(&ipc_futex); |
/* Call asynchronous handler with error code */ |
if (call->callback) |
241,7 → 241,7 |
return; |
} |
if (callid == IPC_CALLRET_TEMPORARY) { |
if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) { |
futex_up(&ipc_futex); |
call->u.msg.phoneid = phoneid; |
309,7 → 309,7 |
callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, |
arg2, arg3, arg4); |
if (callid == IPC_CALLRET_TEMPORARY) { |
if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) { |
if (!call) { |
call = ipc_prepare_async(private, callback); |
if (!call) |
442,7 → 442,7 |
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) { |
if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) { |
break; |
} |
list_remove(&call->list); |
451,7 → 451,7 |
if (call->fid) |
fibril_add_ready(call->fid); |
if (callid == IPC_CALLRET_FATAL) { |
if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) { |
if (call->callback) |
call->callback(call->private, ENOENT, NULL); |
free(call); |
704,7 → 704,7 |
IPC_SET_ARG4(data, arg4); |
IPC_SET_ARG5(data, arg5); |
return __SYSCALL3(SYS_IPC_FORWARD_SLOW, callid, (sysarg_t) &data, mode); |
return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data, mode); |
} |
/** Wrapper for making IPC_M_SHARE_IN calls. |
/branches/network/uspace/lib/libc/generic/devmap.c |
---|
0,0 → 1,264 |
/* |
* Copyright (c) 2007 Josef Cejka |
* Copyright (c) 2009 Jiri Svoboda |
* 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 <string.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/devmap.h> |
#include <devmap.h> |
#include <async.h> |
#include <errno.h> |
static int devmap_phone_driver = -1; |
static int devmap_phone_client = -1; |
/** Get phone to device mapper task. */ |
int devmap_get_phone(devmap_interface_t iface, unsigned int flags) |
{ |
switch (iface) { |
case DEVMAP_DRIVER: |
if (devmap_phone_driver >= 0) |
return devmap_phone_driver; |
if (flags & IPC_FLAG_BLOCKING) |
devmap_phone_driver = ipc_connect_me_to_blocking(PHONE_NS, |
SERVICE_DEVMAP, DEVMAP_DRIVER, 0); |
else |
devmap_phone_driver = ipc_connect_me_to(PHONE_NS, |
SERVICE_DEVMAP, DEVMAP_DRIVER, 0); |
return devmap_phone_driver; |
case DEVMAP_CLIENT: |
if (devmap_phone_client >= 0) |
return devmap_phone_client; |
if (flags & IPC_FLAG_BLOCKING) |
devmap_phone_client = ipc_connect_me_to_blocking(PHONE_NS, |
SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
else |
devmap_phone_client = ipc_connect_me_to(PHONE_NS, |
SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
return devmap_phone_client; |
default: |
return -1; |
} |
} |
void devmap_hangup_phone(devmap_interface_t iface) |
{ |
switch (iface) { |
case DEVMAP_DRIVER: |
if (devmap_phone_driver >= 0) { |
ipc_hangup(devmap_phone_driver); |
devmap_phone_driver = -1; |
} |
break; |
case DEVMAP_CLIENT: |
if (devmap_phone_client >= 0) { |
ipc_hangup(devmap_phone_client); |
devmap_phone_client = -1; |
} |
break; |
default: |
break; |
} |
} |
/** Register new driver with devmap. */ |
int devmap_driver_register(const char *name, async_client_conn_t conn) |
{ |
int phone = devmap_get_phone(DEVMAP_DRIVER, IPC_FLAG_BLOCKING); |
if (phone < 0) |
return phone; |
async_serialize_start(); |
ipc_call_t answer; |
aid_t req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer); |
ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
if (retval != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
return -1; |
} |
async_set_client_connection(conn); |
ipcarg_t callback_phonehash; |
ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash); |
async_wait_for(req, &retval); |
async_serialize_end(); |
return retval; |
} |
/** Register new device. |
* |
* @param name Device name. |
* @param handle Output: Handle to the created instance of device. |
* |
*/ |
int devmap_device_register(const char *name, dev_handle_t *handle) |
{ |
int phone = devmap_get_phone(DEVMAP_DRIVER, IPC_FLAG_BLOCKING); |
if (phone < 0) |
return phone; |
async_serialize_start(); |
ipc_call_t answer; |
aid_t req = async_send_2(phone, DEVMAP_DEVICE_REGISTER, 0, 0, |
&answer); |
ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
if (retval != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
return retval; |
} |
async_wait_for(req, &retval); |
async_serialize_end(); |
if (retval != EOK) { |
if (handle != NULL) |
*handle = -1; |
return retval; |
} |
if (handle != NULL) |
*handle = (dev_handle_t) IPC_GET_ARG1(answer); |
return retval; |
} |
int devmap_device_get_handle(const char *name, dev_handle_t *handle, unsigned int flags) |
{ |
int phone = devmap_get_phone(DEVMAP_CLIENT, flags); |
if (phone < 0) |
return phone; |
async_serialize_start(); |
ipc_call_t answer; |
aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0, |
&answer); |
ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
if (retval != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
return retval; |
} |
async_wait_for(req, &retval); |
async_serialize_end(); |
if (retval != EOK) { |
if (handle != NULL) |
*handle = (dev_handle_t) -1; |
return retval; |
} |
if (handle != NULL) |
*handle = (dev_handle_t) IPC_GET_ARG1(answer); |
return retval; |
} |
int devmap_device_connect(dev_handle_t handle, unsigned int flags) |
{ |
int phone; |
if (flags & IPC_FLAG_BLOCKING) { |
phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, |
DEVMAP_CONNECT_TO_DEVICE, handle); |
} else { |
phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
DEVMAP_CONNECT_TO_DEVICE, handle); |
} |
return phone; |
} |
ipcarg_t devmap_device_get_count(void) |
{ |
int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING); |
if (phone < 0) |
return 0; |
ipcarg_t count; |
int retval = async_req_0_1(phone, DEVMAP_DEVICE_GET_COUNT, &count); |
if (retval != EOK) |
return 0; |
return count; |
} |
ipcarg_t devmap_device_get_devices(ipcarg_t count, dev_desc_t *data) |
{ |
int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING); |
if (phone < 0) |
return 0; |
async_serialize_start(); |
ipc_call_t answer; |
aid_t req = async_send_0(phone, DEVMAP_DEVICE_GET_DEVICES, &answer); |
ipcarg_t retval = ipc_data_read_start(phone, data, count * sizeof(dev_desc_t)); |
if (retval != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
return 0; |
} |
async_wait_for(req, &retval); |
async_serialize_end(); |
if (retval != EOK) |
return 0; |
return IPC_GET_ARG1(answer); |
} |
/branches/network/uspace/lib/libc/generic/async.c |
---|
95,8 → 95,8 |
#include <async.h> |
#include <fibril.h> |
#include <stdio.h> |
#include <libadt/hash_table.h> |
#include <libadt/list.h> |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <ipc/ipc.h> |
#include <assert.h> |
#include <errno.h> |
174,7 → 174,7 |
} connection_t; |
/** Identifier of the incoming connection handled by the current fibril. */ |
__thread connection_t *FIBRIL_connection; |
fibril_local connection_t *FIBRIL_connection; |
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call); |
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call); |
190,11 → 190,9 |
*/ |
static async_client_conn_t interrupt_received = default_interrupt_received; |
static hash_table_t conn_hash_table; |
static LIST_INITIALIZE(timeout_list); |
#define CONN_HASH_TABLE_CHAINS 32 |
/** Compute hash into the connection hash table based on the source phone hash. |
500,9 → 498,10 |
/* Answer all remaining messages with EHANGUP */ |
while (!list_empty(&FIBRIL_connection->msg_queue)) { |
msg_t *msg |
= list_get_instance(FIBRIL_connection->msg_queue.next, msg_t, link); |
msg_t *msg; |
msg = list_get_instance(FIBRIL_connection->msg_queue.next, |
msg_t, link); |
list_remove(&msg->link); |
ipc_answer_0(msg->callid, EHANGUP); |
free(msg); |
563,7 → 562,7 |
} |
/* Add connection to the connection hash table */ |
ipcarg_t key = conn->in_phone_hash; |
unsigned long key = conn->in_phone_hash; |
futex_down(&async_futex); |
hash_table_insert(&conn_hash_table, &key, &conn->link); |
588,23 → 587,28 |
/* Unrouted call - do some default behaviour */ |
if ((callid & IPC_CALLID_NOTIFICATION)) { |
process_notification(callid, call); |
return; |
goto out; |
} |
switch (IPC_GET_METHOD(*call)) { |
case IPC_M_CONNECT_ME: |
case IPC_M_CONNECT_ME_TO: |
/* Open new connection with fibril etc. */ |
async_new_connection(IPC_GET_ARG5(*call), callid, call, |
client_connection); |
return; |
goto out; |
} |
/* Try to route the call through the connection hash table */ |
if (route_call(callid, call)) |
return; |
goto out; |
/* Unknown call from unknown phone - hang it up */ |
ipc_answer_0(callid, EHANGUP); |
return; |
out: |
; |
} |
/** Fire all timeouts that expired. */ |
662,8 → 666,8 |
suseconds_t timeout; |
if (!list_empty(&timeout_list)) { |
awaiter_t *waiter |
= list_get_instance(timeout_list.next, awaiter_t, link); |
awaiter_t *waiter = list_get_instance(timeout_list.next, |
awaiter_t, link); |
struct timeval tv; |
gettimeofday(&tv, NULL); |
680,8 → 684,8 |
futex_up(&async_futex); |
ipc_call_t call; |
ipc_callid_t callid |
= ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE); |
ipc_callid_t callid = ipc_wait_cycle(&call, timeout, |
SYNCH_FLAGS_NONE); |
if (!callid) { |
handle_expired_timeouts(); |
758,13 → 762,13 |
*/ |
static void reply_received(void *arg, int retval, ipc_call_t *data) |
{ |
futex_down(&async_futex); |
amsg_t *msg = (amsg_t *) arg; |
msg->retval = retval; |
futex_down(&async_futex); |
/* Copy data after futex_down, just in case the call was detached */ |
if (msg->dataptr) |
if ((msg->dataptr) && (data)) |
*msg->dataptr = *data; |
write_barrier(); |
810,6 → 814,7 |
msg->done = false; |
msg->dataptr = dataptr; |
msg->wdata.inlist = false; |
/* We may sleep in the next method, but it will use its own mechanism */ |
msg->wdata.active = true; |
849,6 → 854,7 |
msg->done = false; |
msg->dataptr = dataptr; |
msg->wdata.inlist = false; |
/* We may sleep in next method, but it will use its own mechanism */ |
msg->wdata.active = true; |
/branches/network/uspace/lib/libc/generic/vfs/vfs.c |
---|
48,13 → 48,14 |
#include <futex.h> |
#include <errno.h> |
#include <string.h> |
#include <devmap.h> |
#include <ipc/vfs.h> |
#include <ipc/devmap.h> |
#include "../../../srv/vfs/vfs.h" |
int vfs_phone = -1; |
futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
static int vfs_phone = -1; |
static futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
static futex_t cwd_futex = FUTEX_INITIALIZER; |
futex_t cwd_futex = FUTEX_INITIALIZER; |
DIR *cwd_dir = NULL; |
char *cwd_path = NULL; |
size_t cwd_size = 0; |
115,47 → 116,8 |
vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
} |
static int device_get_handle(const char *name, dev_handle_t *handle, |
const unsigned int flags) |
{ |
int phone; |
if (flags & IPC_FLAG_BLOCKING) |
phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
else |
phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
if (phone < 0) |
return phone; |
ipc_call_t answer; |
aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0, |
&answer); |
ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
if (retval != EOK) { |
async_wait_for(req, NULL); |
ipc_hangup(phone); |
return retval; |
} |
async_wait_for(req, &retval); |
if (handle != NULL) |
*handle = -1; |
if (retval == EOK) { |
if (handle != NULL) |
*handle = (dev_handle_t) IPC_GET_ARG1(answer); |
} |
ipc_hangup(phone); |
return retval; |
} |
int mount(const char *fs_name, const char *mp, const char *dev, |
const char *opts, const unsigned int flags) |
const char *opts, unsigned int flags) |
{ |
int res; |
ipcarg_t rc; |
162,7 → 124,7 |
aid_t req; |
dev_handle_t dev_handle; |
res = device_get_handle(dev, &dev_handle, flags); |
res = devmap_device_get_handle(dev, &dev_handle, flags); |
if (res != EOK) |
return res; |
252,6 → 214,7 |
if (rc != EOK) |
return (int) rc; |
return (int) IPC_GET_ARG1(answer); |
} |
260,6 → 223,27 |
return _open(path, L_FILE, oflag); |
} |
int open_node(fdi_node_t *node, int oflag) |
{ |
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
ipc_call_t answer; |
aid_t req = async_send_4(vfs_phone, VFS_OPEN_NODE, node->fs_handle, |
node->dev_handle, node->index, oflag, &answer); |
ipcarg_t rc; |
async_wait_for(req, &rc); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
if (rc != EOK) |
return (int) rc; |
return (int) IPC_GET_ARG1(answer); |
} |
int close(int fildes) |
{ |
ipcarg_t rc; |
330,6 → 314,62 |
return -1; |
} |
int fd_phone(int fildes) |
{ |
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
ipcarg_t device; |
ipcarg_t rc = async_req_1_1(vfs_phone, VFS_DEVICE, fildes, &device); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
if (rc != EOK) |
return -1; |
return devmap_device_connect((dev_handle_t) device, 0); |
} |
int fd_node(int fildes, fdi_node_t *node) |
{ |
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
ipcarg_t fs_handle; |
ipcarg_t dev_handle; |
ipcarg_t index; |
ipcarg_t rc = async_req_1_3(vfs_phone, VFS_NODE, fildes, &fs_handle, |
&dev_handle, &index); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
if (rc == EOK) { |
node->fs_handle = (fs_handle_t) fs_handle; |
node->dev_handle = (dev_handle_t) dev_handle; |
node->index = (fs_index_t) index; |
} |
return rc; |
} |
int fsync(int fildes) |
{ |
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
ipcarg_t rc = async_req_1_0(vfs_phone, VFS_SYNC, fildes); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return (int) rc; |
} |
off_t lseek(int fildes, off_t offset, int whence) |
{ |
ipcarg_t rc; |
/branches/network/uspace/lib/libc/generic/mem.c |
---|
95,7 → 95,7 |
static void *unaligned_memcpy(void *dst, const void *src, size_t n) |
{ |
int i, j; |
size_t i, j; |
struct along *adst = dst; |
const struct along *asrc = src; |
/branches/network/uspace/lib/libc/generic/io/sprintf.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/io/vsprintf.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/io/fprintf.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/io/stream.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/io/stdio.c |
---|
File deleted |
/branches/network/uspace/lib/libc/generic/io/printf.c |
---|
34,20 → 34,40 |
#include <io/printf_core.h> |
#include <stdio.h> |
#include <stdio.h> |
/** Print formatted text. |
* @param fmt format string |
* |
* @param stream Output stream |
* @param fmt Format string |
* |
* \see For more details about format string see printf_core. |
* |
*/ |
int fprintf(FILE *stream, const char *fmt, ...) |
{ |
va_list args; |
va_start(args, fmt); |
int ret = vfprintf(stream, fmt, args); |
va_end(args); |
return ret; |
} |
/** Print formatted text to stdout. |
* |
* @param fmt Format string |
* |
* \see For more details about format string see printf_core. |
* |
*/ |
int printf(const char *fmt, ...) |
{ |
int ret; |
va_list args; |
va_start(args, fmt); |
ret = vprintf(fmt, args); |
int ret = vprintf(fmt, args); |
va_end(args); |
/branches/network/uspace/lib/libc/generic/io/console.c |
---|
0,0 → 1,102 |
/* |
* Copyright (c) 2006 Josef Cejka |
* Copyright (c) 2006 Jakub Vana |
* Copyright (c) 2008 Jiri Svoboda |
* 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. |
*/ |
/** @addtogroup libc |
* @{ |
*/ |
/** @file |
*/ |
#include <libc.h> |
#include <async.h> |
#include <io/console.h> |
#include <ipc/console.h> |
void console_clear(int phone) |
{ |
async_msg_0(phone, CONSOLE_CLEAR); |
} |
int console_get_size(int phone, ipcarg_t *rows, ipcarg_t *cols) |
{ |
return async_req_0_2(phone, CONSOLE_GET_SIZE, rows, cols); |
} |
void console_set_style(int phone, int style) |
{ |
async_msg_1(phone, CONSOLE_SET_STYLE, style); |
} |
void console_set_color(int phone, int fg_color, int bg_color, int flags) |
{ |
async_msg_3(phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags); |
} |
void console_set_rgb_color(int phone, int fg_color, int bg_color) |
{ |
async_msg_2(phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color); |
} |
void console_cursor_visibility(int phone, bool show) |
{ |
async_msg_1(phone, CONSOLE_CURSOR_VISIBILITY, show != false); |
} |
void console_kcon_enable(int phone) |
{ |
async_msg_0(phone, CONSOLE_KCON_ENABLE); |
} |
void console_goto(int phone, ipcarg_t row, ipcarg_t col) |
{ |
async_msg_2(phone, CONSOLE_GOTO, row, col); |
} |
bool console_get_event(int phone, console_event_t *event) |
{ |
ipcarg_t type; |
ipcarg_t key; |
ipcarg_t mods; |
ipcarg_t c; |
int rc = async_req_0_4(phone, CONSOLE_GET_EVENT, &type, &key, &mods, &c); |
if (rc < 0) |
return false; |
event->type = type; |
event->key = key; |
event->mods = mods; |
event->c = c; |
return true; |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/io/snprintf.c |
---|
37,19 → 37,21 |
#include <io/printf_core.h> |
/** Print formatted to the given buffer with limited size. |
* @param str buffer |
* @param size buffer size |
* @param fmt format string |
* |
* @param str Buffer |
* @param size Buffer size |
* @param fmt Format string |
* |
* \see For more details about format string see printf_core. |
* |
*/ |
int snprintf(char *str, size_t size, const char *fmt, ...) |
{ |
int ret; |
va_list args; |
va_start(args, fmt); |
ret = vsnprintf(str, size, fmt, args); |
int ret = vsnprintf(str, size, fmt, args); |
va_end(args); |
return ret; |
/branches/network/uspace/lib/libc/generic/io/asprintf.c |
---|
36,39 → 36,49 |
#include <stdarg.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <io/printf_core.h> |
static int asprintf_prewrite(const char *str, size_t count, void *unused) |
static int asprintf_str_write(const char *str, size_t count, void *unused) |
{ |
return count; |
return str_nlength(str, count); |
} |
static int asprintf_wstr_write(const wchar_t *str, size_t count, void *unused) |
{ |
return wstr_nlength(str, count); |
} |
/** Allocate and print to string. |
* |
* @param strp Address of the pointer where to store the address of |
* the newly allocated string. |
* @fmt Format strin. |
* @fmt Format string. |
* |
* @return Number of characters printed or a negative error code. |
* |
*/ |
int asprintf(char **strp, const char *fmt, ...) |
{ |
struct printf_spec ps = { |
asprintf_prewrite, |
asprintf_str_write, |
asprintf_wstr_write, |
NULL |
}; |
int ret; |
va_list args; |
va_start(args, fmt); |
va_start(args, fmt); |
ret = printf_core(fmt, &ps, args); |
int ret = printf_core(fmt, &ps, args); |
va_end(args); |
if (ret > 0) { |
*strp = malloc(ret + 20); |
if (!*strp) |
*strp = malloc(STR_BOUNDS(ret) + 1); |
if (*strp == NULL) |
return -1; |
va_start(args, fmt); |
vsprintf(*strp, fmt, args); |
vsnprintf(*strp, STR_BOUNDS(ret) + 1, fmt, args); |
va_end(args); |
} |
/branches/network/uspace/lib/libc/generic/io/io.c |
---|
32,96 → 32,549 |
/** @file |
*/ |
#include <libc.h> |
#include <stdio.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <io/io.h> |
#include <fcntl.h> |
#include <assert.h> |
#include <string.h> |
#include <errno.h> |
#include <console.h> |
#include <bool.h> |
#include <malloc.h> |
#include <io/klog.h> |
#include <vfs/vfs.h> |
#include <ipc/devmap.h> |
#include <adt/list.h> |
const static char nl = '\n'; |
static void _fflushbuf(FILE *stream); |
int puts(const char *str) |
static FILE stdin_null = { |
.fd = -1, |
.error = true, |
.eof = true, |
.klog = false, |
.phone = -1, |
.btype = _IONBF, |
.buf = NULL, |
.buf_size = 0, |
.buf_head = NULL |
}; |
static FILE stdout_klog = { |
.fd = -1, |
.error = false, |
.eof = false, |
.klog = true, |
.phone = -1, |
.btype = _IOLBF, |
.buf = NULL, |
.buf_size = BUFSIZ, |
.buf_head = NULL |
}; |
static FILE stderr_klog = { |
.fd = -1, |
.error = false, |
.eof = false, |
.klog = true, |
.phone = -1, |
.btype = _IONBF, |
.buf = NULL, |
.buf_size = 0, |
.buf_head = NULL |
}; |
FILE *stdin = NULL; |
FILE *stdout = NULL; |
FILE *stderr = NULL; |
static LIST_INITIALIZE(files); |
void stdio_init(int filc, fdi_node_t *filv[]) |
{ |
size_t count; |
if (filc > 0) { |
stdin = fopen_node(filv[0], "r"); |
} else { |
stdin = &stdin_null; |
list_append(&stdin->link, &files); |
} |
if (str == NULL) |
return putnchars("(NULL)", 6); |
if (filc > 1) { |
stdout = fopen_node(filv[1], "w"); |
} else { |
stdout = &stdout_klog; |
list_append(&stdout->link, &files); |
} |
for (count = 0; str[count] != 0; count++); |
if (filc > 2) { |
stderr = fopen_node(filv[2], "w"); |
} else { |
stderr = &stderr_klog; |
list_append(&stderr->link, &files); |
} |
} |
if (console_write((void *) str, count) == count) { |
if (console_write(&nl, 1) == 1) |
void stdio_done(void) |
{ |
link_t *link = files.next; |
while (link != &files) { |
FILE *file = list_get_instance(link, FILE, link); |
fclose(file); |
link = files.next; |
} |
} |
static bool parse_mode(const char *mode, int *flags) |
{ |
/* Parse mode except first character. */ |
const char *mp = mode; |
if (*mp++ == 0) { |
errno = EINVAL; |
return false; |
} |
if ((*mp == 'b') || (*mp == 't')) |
mp++; |
bool plus; |
if (*mp == '+') { |
mp++; |
plus = true; |
} else |
plus = false; |
if (*mp != 0) { |
errno = EINVAL; |
return false; |
} |
/* Parse first character of mode and determine flags for open(). */ |
switch (mode[0]) { |
case 'r': |
*flags = plus ? O_RDWR : O_RDONLY; |
break; |
case 'w': |
*flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
break; |
case 'a': |
/* TODO: a+ must read from beginning, append to the end. */ |
if (plus) { |
errno = ENOTSUP; |
return false; |
} |
*flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
default: |
errno = EINVAL; |
return false; |
} |
return true; |
} |
/** Set stream buffer. */ |
void setvbuf(FILE *stream, void *buf, int mode, size_t size) |
{ |
stream->btype = mode; |
stream->buf = buf; |
stream->buf_size = size; |
stream->buf_head = stream->buf; |
} |
/** Allocate stream buffer. */ |
static int _fallocbuf(FILE *stream) |
{ |
assert(stream->buf == NULL); |
stream->buf = malloc(stream->buf_size); |
if (stream->buf == NULL) { |
errno = ENOMEM; |
return -1; |
} |
stream->buf_head = stream->buf; |
return 0; |
} |
/** Open a stream. |
* |
* @param path Path of the file to open. |
* @param mode Mode string, (r|w|a)[b|t][+]. |
* |
*/ |
FILE *fopen(const char *path, const char *mode) |
{ |
int flags; |
if (!parse_mode(mode, &flags)) |
return NULL; |
/* Open file. */ |
FILE *stream = malloc(sizeof(FILE)); |
if (stream == NULL) { |
errno = ENOMEM; |
return NULL; |
} |
stream->fd = open(path, flags, 0666); |
if (stream->fd < 0) { |
/* errno was set by open() */ |
free(stream); |
return NULL; |
} |
stream->error = false; |
stream->eof = false; |
stream->klog = false; |
stream->phone = -1; |
/* FIXME: Should select buffering type based on what was opened. */ |
setvbuf(stream, NULL, _IOFBF, BUFSIZ); |
list_append(&stream->link, &files); |
return stream; |
} |
FILE *fdopen(int fd, const char *mode) |
{ |
/* Open file. */ |
FILE *stream = malloc(sizeof(FILE)); |
if (stream == NULL) { |
errno = ENOMEM; |
return NULL; |
} |
stream->fd = fd; |
stream->error = false; |
stream->eof = false; |
stream->klog = false; |
stream->phone = -1; |
/* FIXME: Should select buffering type based on what was opened. */ |
setvbuf(stream, NULL, _IOLBF, BUFSIZ); |
list_append(&stream->link, &files); |
return stream; |
} |
FILE *fopen_node(fdi_node_t *node, const char *mode) |
{ |
int flags; |
if (!parse_mode(mode, &flags)) |
return NULL; |
/* Open file. */ |
FILE *stream = malloc(sizeof(FILE)); |
if (stream == NULL) { |
errno = ENOMEM; |
return NULL; |
} |
stream->fd = open_node(node, flags); |
if (stream->fd < 0) { |
/* errno was set by open_node() */ |
free(stream); |
return NULL; |
} |
stream->error = false; |
stream->eof = false; |
stream->klog = false; |
stream->phone = -1; |
/* FIXME: Should select buffering type based on what was opened. */ |
setvbuf(stream, NULL, _IOLBF, BUFSIZ); |
list_append(&stream->link, &files); |
return stream; |
} |
int fclose(FILE *stream) |
{ |
int rc = 0; |
fflush(stream); |
if (stream->phone >= 0) |
ipc_hangup(stream->phone); |
if (stream->fd >= 0) |
rc = close(stream->fd); |
list_remove(&stream->link); |
if ((stream != &stdin_null) |
&& (stream != &stdout_klog) |
&& (stream != &stderr_klog)) |
free(stream); |
stream = NULL; |
if (rc != 0) { |
/* errno was set by close() */ |
return EOF; |
} |
/** Put count chars from buffer to stdout without adding newline |
* @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
* @param count |
* @return 0 on succes, EOF on fail |
return 0; |
} |
/** Read from a stream. |
* |
* @param buf Destination buffer. |
* @param size Size of each record. |
* @param nmemb Number of records to read. |
* @param stream Pointer to the stream. |
* |
*/ |
int putnchars(const char *buf, size_t count) |
size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream) |
{ |
if (console_write((void *) buf, count) == count) |
return 0; |
size_t left = size * nmemb; |
size_t done = 0; |
return EOF; |
/* Make sure no data is pending write. */ |
_fflushbuf(stream); |
while ((left > 0) && (!stream->error) && (!stream->eof)) { |
ssize_t rd = read(stream->fd, buf + done, left); |
if (rd < 0) |
stream->error = true; |
else if (rd == 0) |
stream->eof = true; |
else { |
left -= rd; |
done += rd; |
} |
} |
/** Same as puts, but does not print newline at end |
return (done / size); |
} |
static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) |
{ |
size_t left = size * nmemb; |
size_t done = 0; |
while ((left > 0) && (!stream->error)) { |
ssize_t wr; |
if (stream->klog) |
wr = klog_write(buf + done, left); |
else |
wr = write(stream->fd, buf + done, left); |
if (wr <= 0) |
stream->error = true; |
else { |
left -= wr; |
done += wr; |
} |
} |
return (done / size); |
} |
/** Drain stream buffer, do not sync stream. */ |
static void _fflushbuf(FILE *stream) |
{ |
size_t bytes_used; |
if (!stream->buf || stream->btype == _IONBF || stream->error) |
return; |
bytes_used = stream->buf_head - stream->buf; |
if (bytes_used == 0) |
return; |
(void) _fwrite(stream->buf, 1, bytes_used, stream); |
stream->buf_head = stream->buf; |
} |
/** Write to a stream. |
* |
* @param buf Source buffer. |
* @param size Size of each record. |
* @param nmemb Number of records to write. |
* @param stream Pointer to the stream. |
* |
*/ |
int putstr(const char *str) |
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) |
{ |
size_t count; |
uint8_t *data; |
size_t bytes_left; |
size_t now; |
size_t buf_free; |
size_t total_written; |
size_t i; |
uint8_t b; |
bool need_flush; |
if (str == NULL) |
return putnchars("(NULL)", 6); |
/* If not buffered stream, write out directly. */ |
if (stream->btype == _IONBF) |
return _fwrite(buf, size, nmemb, stream); |
for (count = 0; str[count] != 0; count++); |
if (console_write((void *) str, count) == count) |
return 0; |
/* Perform lazy allocation of stream buffer. */ |
if (stream->buf == NULL) { |
if (_fallocbuf(stream) != 0) |
return 0; /* Errno set by _fallocbuf(). */ |
} |
return EOF; |
data = (uint8_t *) buf; |
bytes_left = size * nmemb; |
total_written = 0; |
need_flush = false; |
while (!stream->error && bytes_left > 0) { |
buf_free = stream->buf_size - (stream->buf_head - stream->buf); |
if (bytes_left > buf_free) |
now = buf_free; |
else |
now = bytes_left; |
for (i = 0; i < now; i++) { |
b = data[i]; |
stream->buf_head[i] = b; |
if (b == '\n' && stream->btype == _IOLBF) |
need_flush = true; |
} |
int putchar(int c) |
buf += now; |
stream->buf_head += now; |
buf_free -= now; |
bytes_left -= now; |
total_written += now; |
if (buf_free == 0) { |
/* Only need to drain buffer. */ |
_fflushbuf(stream); |
need_flush = false; |
} |
} |
if (need_flush) |
fflush(stream); |
return (total_written / size); |
} |
int fputc(wchar_t c, FILE *stream) |
{ |
char buf[STR_BOUNDS(1)]; |
size_t offs; |
size_t sz = 0; |
offs = 0; |
if (chr_encode(c, buf, &offs, STR_BOUNDS(1)) != EOK) |
if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) { |
size_t wr = fwrite(buf, sz, 1, stream); |
if (wr < sz) |
return EOF; |
if (console_write((void *) buf, offs) == offs) |
return c; |
return (int) c; |
} |
return EOF; |
} |
int getchar(void) |
int putchar(wchar_t c) |
{ |
unsigned char c; |
return fputc(c, stdout); |
} |
console_flush(); |
if (read_stdin((void *) &c, 1) == 1) |
return c; |
int fputs(const char *str, FILE *stream) |
{ |
return fwrite(str, str_size(str), 1, stream); |
} |
int puts(const char *str) |
{ |
return fputs(str, stdout); |
} |
int fgetc(FILE *stream) |
{ |
char c; |
/* This could be made faster by only flushing when needed. */ |
if (stdout) |
fflush(stdout); |
if (stderr) |
fflush(stderr); |
if (fread(&c, sizeof(char), 1, stream) < sizeof(char)) |
return EOF; |
return (int) c; |
} |
int fflush(FILE *f) |
int getchar(void) |
{ |
/* Dummy implementation */ |
(void) f; |
console_flush(); |
return fgetc(stdin); |
} |
int fseek(FILE *stream, long offset, int origin) |
{ |
off_t rc = lseek(stream->fd, offset, origin); |
if (rc == (off_t) (-1)) { |
/* errno has been set by lseek. */ |
return -1; |
} |
stream->eof = false; |
return 0; |
} |
void rewind(FILE *stream) |
{ |
(void) fseek(stream, 0, SEEK_SET); |
} |
int fflush(FILE *stream) |
{ |
_fflushbuf(stream); |
if (stream->klog) { |
klog_update(); |
return EOK; |
} |
if (stream->fd >= 0) |
return fsync(stream->fd); |
return ENOENT; |
} |
int feof(FILE *stream) |
{ |
return stream->eof; |
} |
int ferror(FILE *stream) |
{ |
return stream->error; |
} |
int fphone(FILE *stream) |
{ |
if (stream->fd >= 0) { |
if (stream->phone < 0) |
stream->phone = fd_phone(stream->fd); |
return stream->phone; |
} |
return -1; |
} |
int fnode(FILE *stream, fdi_node_t *node) |
{ |
if (stream->fd >= 0) |
return fd_node(stream->fd, node); |
return ENOENT; |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/io/vprintf.c |
---|
38,37 → 38,25 |
#include <io/printf_core.h> |
#include <futex.h> |
#include <async.h> |
#include <console.h> |
#include <string.h> |
static atomic_t printf_futex = FUTEX_INITIALIZER; |
static int vprintf_str_write(const char *str, size_t size, void *data) |
static int vprintf_str_write(const char *str, size_t size, void *stream) |
{ |
size_t offset = 0; |
size_t prev; |
count_t chars = 0; |
while (offset < size) { |
prev = offset; |
str_decode(str, &offset, size); |
console_write(str + prev, offset - prev); |
chars++; |
size_t wr = fwrite(str, 1, size, (FILE *) stream); |
return str_nlength(str, wr); |
} |
return chars; |
} |
static int vprintf_wstr_write(const wchar_t *str, size_t size, void *data) |
static int vprintf_wstr_write(const wchar_t *str, size_t size, void *stream) |
{ |
size_t offset = 0; |
size_t boff; |
count_t chars = 0; |
char buf[4]; |
size_t chars = 0; |
while (offset < size) { |
boff = 0; |
chr_encode(str[chars], buf, &boff, 4); |
console_write(buf, boff); |
if (fputc(str[chars], (FILE *) stream) <= 0) |
break; |
chars++; |
offset += sizeof(wchar_t); |
} |
76,33 → 64,55 |
return chars; |
} |
/** Print formatted text. |
* @param fmt format string |
* @param ap format parameters |
* |
* @param stream Output stream |
* @param fmt Format string |
* @param ap Format parameters |
* |
* \see For more details about format string see printf_core. |
* |
*/ |
int vprintf(const char *fmt, va_list ap) |
int vfprintf(FILE *stream, const char *fmt, va_list ap) |
{ |
struct printf_spec ps = { |
vprintf_str_write, |
vprintf_wstr_write, |
NULL |
stream |
}; |
/* |
* Prevent other threads to execute printf_core() |
*/ |
futex_down(&printf_futex); |
/* |
* Prevent other pseudo threads of the same thread |
* Prevent other fibrils of the same thread |
* to execute printf_core() |
*/ |
async_serialize_start(); |
int ret = printf_core(fmt, &ps, ap); |
async_serialize_end(); |
futex_up(&printf_futex); |
return ret; |
} |
/** Print formatted text to stdout. |
* |
* @param file Output stream |
* @param fmt Format string |
* @param ap Format parameters |
* |
* \see For more details about format string see printf_core. |
* |
*/ |
int vprintf(const char *fmt, va_list ap) |
{ |
return vfprintf(stdout, fmt, ap); |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/io/vsnprintf.c |
---|
82,7 → 82,7 |
* with the trailing zero => print only a part |
* of string |
*/ |
index_t index = 0; |
size_t index = 0; |
while (index < size) { |
wchar_t uc = str_decode(str, &index, size); |
130,7 → 130,7 |
*/ |
static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
{ |
index_t index = 0; |
size_t index = 0; |
while (index < (size / sizeof(wchar_t))) { |
size_t left = data->size - data->len; |
/branches/network/uspace/lib/libc/generic/io/printf_core.c |
---|
173,7 → 173,7 |
*/ |
static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps) |
{ |
count_t counter = 0; |
size_t counter = 0; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (--width > 0) { |
/* |
211,7 → 211,7 |
*/ |
static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps) |
{ |
count_t counter = 0; |
size_t counter = 0; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (--width > 0) { |
/* |
254,12 → 254,12 |
return printf_putstr(nullstr, ps); |
/* Print leading spaces. */ |
count_t strw = str_length(str); |
size_t strw = str_length(str); |
if (precision == 0) |
precision = strw; |
/* Left padding */ |
count_t counter = 0; |
size_t counter = 0; |
width -= precision; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (width-- > 0) { |
301,9 → 301,6 |
if (str == NULL) |
return printf_putstr(nullstr, ps); |
if (*str == U_BOM) |
str++; |
/* Print leading spaces. */ |
size_t strw = wstr_length(str); |
if (precision == 0) |
310,7 → 307,7 |
precision = strw; |
/* Left padding */ |
count_t counter = 0; |
size_t counter = 0; |
width -= precision; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (width-- > 0) { |
432,7 → 429,7 |
} |
width -= precision + size - number_size; |
count_t counter = 0; |
size_t counter = 0; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (width-- > 0) { |
595,7 → 592,7 |
size_t nxt = 0; /* Index of the next character from fmt */ |
size_t j = 0; /* Index to the first not printed nonformating character */ |
count_t counter = 0; /* Number of characters printed */ |
size_t counter = 0; /* Number of characters printed */ |
int retval; /* Return values from nested functions */ |
while (true) { |
/branches/network/uspace/lib/libc/generic/io/klog.c |
---|
0,0 → 1,53 |
/* |
* Copyright (c) 2006 Josef Cejka |
* Copyright (c) 2006 Jakub Vana |
* 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. |
*/ |
/** @addtogroup libc |
* @{ |
*/ |
/** @file |
*/ |
#include <libc.h> |
#include <string.h> |
#include <sys/types.h> |
#include <unistd.h> |
#include <io/klog.h> |
size_t klog_write(const void *buf, size_t size) |
{ |
return (size_t) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, size); |
} |
void klog_update(void) |
{ |
(void) __SYSCALL3(SYS_KLOG, 1, NULL, 0); |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/adt/hash_table.c |
---|
0,0 → 1,196 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* 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. |
*/ |
/** @addtogroup libc |
* @{ |
*/ |
/** @file |
*/ |
/* |
* This is an implementation of generic chained hash table. |
*/ |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <unistd.h> |
#include <malloc.h> |
#include <assert.h> |
#include <stdio.h> |
#include <string.h> |
/** Create chained hash table. |
* |
* @param h Hash table structure. Will be initialized by this call. |
* @param m Number of hash table buckets. |
* @param max_keys Maximal number of keys needed to identify an item. |
* @param op Hash table operations structure. |
* @return True on success |
*/ |
int hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys, |
hash_table_operations_t *op) |
{ |
hash_count_t i; |
assert(h); |
assert(op && op->hash && op->compare); |
assert(max_keys > 0); |
h->entry = malloc(m * sizeof(link_t)); |
if (!h->entry) { |
printf("cannot allocate memory for hash table\n"); |
return false; |
} |
memset((void *) h->entry, 0, m * sizeof(link_t)); |
for (i = 0; i < m; i++) |
list_initialize(&h->entry[i]); |
h->entries = m; |
h->max_keys = max_keys; |
h->op = op; |
return true; |
} |
/** Destroy a hash table instance. |
* |
* @param h Hash table to be destroyed. |
*/ |
void hash_table_destroy(hash_table_t *h) |
{ |
assert(h); |
assert(h->entry); |
free(h->entry); |
} |
/** Insert item into a hash table. |
* |
* @param h Hash table. |
* @param key Array of all keys necessary to compute hash index. |
* @param item Item to be inserted into the hash table. |
*/ |
void hash_table_insert(hash_table_t *h, unsigned long key[], link_t *item) |
{ |
hash_index_t chain; |
assert(item); |
assert(h && h->op && h->op->hash && h->op->compare); |
chain = h->op->hash(key); |
assert(chain < h->entries); |
list_append(item, &h->entry[chain]); |
} |
/** Search hash table for an item matching keys. |
* |
* @param h Hash table. |
* @param key Array of all keys needed to compute hash index. |
* |
* @return Matching item on success, NULL if there is no such item. |
*/ |
link_t *hash_table_find(hash_table_t *h, unsigned long key[]) |
{ |
link_t *cur; |
hash_index_t chain; |
assert(h && h->op && h->op->hash && h->op->compare); |
chain = h->op->hash(key); |
assert(chain < h->entries); |
for (cur = h->entry[chain].next; cur != &h->entry[chain]; |
cur = cur->next) { |
if (h->op->compare(key, h->max_keys, cur)) { |
/* |
* The entry is there. |
*/ |
return cur; |
} |
} |
return NULL; |
} |
/** Remove all matching items from hash table. |
* |
* For each removed item, h->remove_callback() is called. |
* |
* @param h Hash table. |
* @param key Array of keys that will be compared against items of |
* the hash table. |
* @param keys Number of keys in the 'key' array. |
*/ |
void hash_table_remove(hash_table_t *h, unsigned long key[], hash_count_t keys) |
{ |
hash_index_t chain; |
link_t *cur; |
assert(h && h->op && h->op->hash && h->op->compare && |
h->op->remove_callback); |
assert(keys <= h->max_keys); |
if (keys == h->max_keys) { |
/* |
* All keys are known, hash_table_find() can be used to find the |
* entry. |
*/ |
cur = hash_table_find(h, key); |
if (cur) { |
list_remove(cur); |
h->op->remove_callback(cur); |
} |
return; |
} |
/* |
* Fewer keys were passed. |
* Any partially matching entries are to be removed. |
*/ |
for (chain = 0; chain < h->entries; chain++) { |
for (cur = h->entry[chain].next; cur != &h->entry[chain]; |
cur = cur->next) { |
if (h->op->compare(key, keys, cur)) { |
link_t *hlp; |
hlp = cur; |
cur = cur->prev; |
list_remove(hlp); |
h->op->remove_callback(hlp); |
continue; |
} |
} |
} |
} |
/** @} |
*/ |
/branches/network/uspace/lib/libc/generic/adt/list.c |
---|
0,0 → 1,112 |
/* |
* Copyright (c) 2004 Jakub Jermar |
* 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. |
*/ |
/** @addtogroup libc |
* @{ |
*/ |
/** @file |
*/ |
#include <adt/list.h> |
/** Check for membership |
* |
* Check whether link is contained in the list head. |
* The membership is defined as pointer equivalence. |
* |
* @param link Item to look for. |
* @param head List to look in. |
* |
* @return true if link is contained in head, false otherwise. |
* |
*/ |
int list_member(const link_t *link, const link_t *head) |
{ |
int found = 0; |
link_t *hlp = head->next; |
while (hlp != head) { |
if (hlp == link) { |
found = 1; |
break; |
} |
hlp = hlp->next; |
} |
return found; |
} |
/** Concatenate two lists |
* |
* Concatenate lists head1 and head2, producing a single |
* list head1 containing items from both (in head1, head2 |
* order) and empty list head2. |
* |
* @param head1 First list and concatenated output |
* @param head2 Second list and empty output. |
* |
*/ |
void list_concat(link_t *head1, link_t *head2) |
{ |
if (list_empty(head2)) |
return; |
head2->next->prev = head1->prev; |
head2->prev->next = head1; |
head1->prev->next = head2->next; |
head1->prev = head2->prev; |
list_initialize(head2); |
} |
/** Count list items |
* |
* Return the number of items in the list. |
* |
* @param link List to count. |
* |
* @return Number of items in the list. |
* |
*/ |
unsigned int list_count(const link_t *link) |
{ |
unsigned int count = 0; |
link_t *hlp = link->next; |
while (hlp != link) { |
count++; |
hlp = hlp->next; |
} |
return count; |
} |
/** @} |
*/ |