user/lib/libc/src/file/fd/map.c
/**
 * Registers a stream with the file descriptor map. A fake file descriptor number is allocated for
 * the stream and stored in the structure.
 *
 * @return 0 on success, error code otherwise.
 */
Registers a stream with the file descriptor map. A fake file descriptor number is allocated for
the stream and stored in the structure.

@return 0 on success, error code otherwise.
Registers a stream with the file descriptor map . A fake file descriptor number is allocated for the stream and stored in the structure . @return 0 on success error code otherwise .
int RegisterFdStream(stream_t *stream, bool allocateFd) {
    int err;
    if(allocateFd) {
        int num = __atomic_add_fetch(&gState.nextFd, 1, __ATOMIC_RELAXED);
        if(num <= 0) {
            fprintf(stderr, "fd number overflow!\n");
            abort();
        }
        stream->fd = num;
    }
    err = mtx_lock(&gState.lock);
    if(err != thrd_success) abort();
    fd_map_entry_t info = {
        .key    = stream->fd,
        .stream = stream
    };
    void *ret = hashmap_set(gState.entries, &info);
    if(ret || hashmap_oom(gState.entries)) {
        err = -1;
        goto beach;
    }
    err = 0;
beach:;
    mtx_unlock(&gState.lock);
    return err;
}
int RegisterFdStream ( stream_t * stream , bool allocateFd ) { int err ; if ( allocateFd ) { int num = __atomic_add_fetch ( & gState . nextFd , 1 , __ATOMIC_RELAXED ) ; if ( num <= 0 ) { fprintf ( stderr , " \n " ) ; abort ( ) ; } stream -> fd = num ; } err = mtx_lock ( & gState . lock ) ; if ( err != thrd_success ) abort ( ) ; fd_map_entry_t info = { . key = stream -> fd , . stream = stream } ; void * ret = hashmap_set ( gState . entries , & info ) ; if ( ret || hashmap_oom ( gState . entries ) ) { err = -1 ; goto beach ; } err = 0 ; beach : ; mtx_unlock ( & gState . lock ) ; return err ; }
Registers a stream with the file descriptor map.
Registers a stream with the file descriptor map .
// allocate a file descriptor number // acquire lock // register into the map // either an item was replaced, or we ran out of memory // we've successfully inserted the item // and unlock