Changeset 7473807 in mainline


Ignore:
Timestamp:
2018-05-11T20:22:42Z (6 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d2c5159
Parents:
ae89656
Message:

Use atomic malloc allocations

We can safely use atomic allocations in places that use the non-failing
version of malloc(), but test the return value anyway. And also in some
places that can afford to return failure but did not because of comfort.

Files:
22 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/ia64/src/ddi/ddi.c

    rae89656 r7473807  
    5656{
    5757        if (!task->arch.iomap) {
    58                 task->arch.iomap = malloc(sizeof(bitmap_t), 0);
     58                task->arch.iomap = malloc(sizeof(bitmap_t), FRAME_ATOMIC);
    5959                if (task->arch.iomap == NULL)
    6060                        return ENOMEM;
    6161
    62                 void *store = malloc(bitmap_size(IO_MEMMAP_PAGES), 0);
     62                void *store = malloc(bitmap_size(IO_MEMMAP_PAGES),
     63                    FRAME_ATOMIC);
    6364                if (store == NULL)
    6465                        return ENOMEM;
  • kernel/genarch/src/drivers/ega/ega.c

    rae89656 r7473807  
    609609        }
    610610
    611         instance->backbuf = (uint8_t *) malloc(EGA_VRAM_SIZE, 0);
     611        instance->backbuf = (uint8_t *) malloc(EGA_VRAM_SIZE, FRAME_ATOMIC);
    612612        if (!instance->backbuf) {
    613613                LOG("Unable to allocate backbuffer.");
  • kernel/genarch/src/fb/fb.c

    rae89656 r7473807  
    618618        }
    619619
    620         instance->backbuf = (uint16_t *) malloc(bbsize, 0);
     620        instance->backbuf = (uint16_t *) malloc(bbsize, FRAME_ATOMIC);
    621621        if (!instance->backbuf) {
    622622                LOG("Unable to allocate backbuffer.");
     
    626626        }
    627627
    628         instance->glyphs = (uint8_t *) malloc(glyphsize, 0);
     628        instance->glyphs = (uint8_t *) malloc(glyphsize, FRAME_ATOMIC);
    629629        if (!instance->glyphs) {
    630630                LOG("Unable to allocate glyphs.");
     
    635635        }
    636636
    637         instance->bgscan = malloc(instance->bgscanbytes, 0);
     637        instance->bgscan = malloc(instance->bgscanbytes, FRAME_ATOMIC);
    638638        if (!instance->bgscan) {
    639639                LOG("Unable to allocate background pixel.");
  • kernel/generic/include/synch/rcu.h

    rae89656 r7473807  
    5555 *
    5656 * // Insert at the beginning of the list.
    57  * exam_t *my_exam = malloc(sizeof(exam_t), 0);
     57 * exam_t *my_exam = malloc(sizeof(exam_t), FRAME_ATOMIC);
    5858 * my_exam->grade = 5;
    5959 * my_exam->next = exam_list;
  • kernel/generic/src/console/cmd.c

    rae89656 r7473807  
    14651465                return true;
    14661466
    1467         uint64_t *data = (uint64_t *) malloc(sizeof(uint64_t) * cnt, 0);
     1467        uint64_t *data = (uint64_t *) malloc(sizeof(uint64_t) * cnt,
     1468            FRAME_ATOMIC);
    14681469        if (data == NULL) {
    14691470                printf("Error allocating memory for statistics\n");
  • kernel/generic/src/console/console.c

    rae89656 r7473807  
    411411
    412412        if (size > 0) {
    413                 data = (char *) malloc(size + 1, 0);
     413                data = (char *) malloc(size + 1, FRAME_ATOMIC);
    414414                if (!data)
    415415                        return (sys_errno_t) ENOMEM;
  • kernel/generic/src/console/kconsole.c

    rae89656 r7473807  
    298298                end++;
    299299
    300         tmp = malloc(STR_BOUNDS(end - start + 1), 0);
     300        tmp = malloc(STR_BOUNDS(end - start + 1), FRAME_ATOMIC);
     301        if (!tmp)
     302                return NULL;
    301303
    302304        wstr_to_str(tmp, end - start + 1, &cmdline[start]);
  • kernel/generic/src/ipc/irq.c

    rae89656 r7473807  
    8282{
    8383        /* Copy the physical base addresses aside. */
    84         uintptr_t *pbase = malloc(rangecount * sizeof(uintptr_t), 0);
     84        uintptr_t *pbase = malloc(rangecount * sizeof(uintptr_t), FRAME_ATOMIC);
     85        if (!pbase)
     86                return ENOMEM;
    8587        for (size_t i = 0; i < rangecount; i++)
    8688                pbase[i] = ranges[i].base;
     
    225227        irq_cmd_t *cmds = NULL;
    226228
    227         irq_code_t *code = malloc(sizeof(*code), 0);
     229        irq_code_t *code = malloc(sizeof(*code), FRAME_ATOMIC);
     230        if (!code)
     231                return NULL;
    228232        errno_t rc = copy_from_uspace(code, ucode, sizeof(*code));
    229233        if (rc != EOK)
     
    234238                goto error;
    235239
    236         ranges = malloc(sizeof(code->ranges[0]) * code->rangecount, 0);
     240        ranges = malloc(sizeof(code->ranges[0]) * code->rangecount,
     241            FRAME_ATOMIC);
     242        if (!ranges)
     243                goto error;
    237244        rc = copy_from_uspace(ranges, code->ranges,
    238245            sizeof(code->ranges[0]) * code->rangecount);
     
    240247                goto error;
    241248
    242         cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
     249        cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, FRAME_ATOMIC);
     250        if (!cmds)
     251                goto error;
    243252        rc = copy_from_uspace(cmds, code->cmds,
    244253            sizeof(code->cmds[0]) * code->cmdcount);
  • kernel/generic/src/ipc/ops/dataread.c

    rae89656 r7473807  
    7575                        IPC_SET_ARG1(answer->data, dst);
    7676
    77                         answer->buffer = malloc(size, 0);
     77                        answer->buffer = malloc(size, FRAME_ATOMIC);
     78                        if (!answer->buffer) {
     79                                IPC_SET_RETVAL(answer->data, ENOMEM);
     80                                return EOK;
     81                        }
    7882                        errno_t rc = copy_from_uspace(answer->buffer,
    7983                            (void *) src, size);
     
    8488                                 * ipc_call_free().
    8589                                 */
     90                                return EOK;
    8691                        }
    8792                } else if (!size) {
  • kernel/generic/src/ipc/ops/datawrite.c

    rae89656 r7473807  
    5656        }
    5757
    58         call->buffer = (uint8_t *) malloc(size, 0);
     58        call->buffer = (uint8_t *) malloc(size, FRAME_ATOMIC);
     59        if (!call->buffer)
     60                return ENOMEM;
    5961        errno_t rc = copy_from_uspace(call->buffer, (void *) src, size);
    6062        if (rc != EOK) {
  • kernel/generic/src/lib/gsort.c

    rae89656 r7473807  
    111111
    112112        if (elem_size > IBUF_SIZE) {
    113                 slot = (void *) malloc(elem_size, 0);
     113                slot = (void *) malloc(elem_size, FRAME_ATOMIC);
    114114                if (!slot)
    115115                        return false;
  • kernel/generic/src/log/log.c

    rae89656 r7473807  
    307307        switch (operation) {
    308308        case KLOG_WRITE:
    309                 data = (char *) malloc(size + 1, 0);
     309                data = (char *) malloc(size + 1, FRAME_ATOMIC);
    310310                if (!data)
    311311                        return (sys_errno_t) ENOMEM;
     
    326326                return EOK;
    327327        case KLOG_READ:
    328                 data = (char *) malloc(size, 0);
     328                data = (char *) malloc(size, FRAME_ATOMIC);
    329329                if (!data)
    330330                        return (sys_errno_t) ENOMEM;
  • kernel/generic/src/main/kinit.c

    rae89656 r7473807  
    209209                size_t arguments_size = str_size(arguments);
    210210
    211                 void *arguments_copy = malloc(arguments_size, 0);
     211                void *arguments_copy = malloc(arguments_size, FRAME_ATOMIC);
    212212                if (arguments_copy == NULL)
    213213                        continue;
  • kernel/generic/src/mm/as.c

    rae89656 r7473807  
    620620        }
    621621
    622         as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t), 0);
     622        as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t), FRAME_ATOMIC);
     623        if (!area) {
     624                mutex_unlock(&as->lock);
     625                return NULL;
     626        }
    623627
    624628        mutex_initialize(&area->lock, MUTEX_PASSIVE);
     
    646650         */
    647651        if (!(attrs & AS_AREA_ATTR_PARTIAL)) {
    648                 si = (share_info_t *) malloc(sizeof(share_info_t), 0);
     652                si = (share_info_t *) malloc(sizeof(share_info_t),
     653                    FRAME_ATOMIC);
     654                if (!si) {
     655                        free(area);
     656                        mutex_unlock(&as->lock);
     657                        return NULL;
     658                }
    649659                mutex_initialize(&si->lock, MUTEX_PASSIVE);
    650660                si->refcount = 1;
     
    12921302
    12931303        /* An array for storing frame numbers */
    1294         uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t), 0);
     1304        uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t),
     1305            FRAME_ATOMIC);
     1306        if (!old_frame) {
     1307                mutex_unlock(&area->lock);
     1308                mutex_unlock(&as->lock);
     1309                return ENOMEM;
     1310        }
    12951311
    12961312        page_table_lock(as, false);
  • kernel/generic/src/mm/backend_phys.c

    rae89656 r7473807  
    160160                phys_shared_data_t *data;
    161161
    162                 data = (phys_shared_data_t *) malloc(sizeof(*data), 0);
     162                data = (phys_shared_data_t *) malloc(sizeof(*data),
     163                    FRAME_ATOMIC);
     164                if (!data)
     165                        return false;
    163166
    164167                data->base = area->backend_data.base;
  • kernel/generic/src/proc/program.c

    rae89656 r7473807  
    7171errno_t program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg)
    7272{
     73        uspace_arg_t *kernel_uarg = (uspace_arg_t *)
     74            malloc(sizeof(uspace_arg_t), FRAME_ATOMIC);
     75        if (!kernel_uarg)
     76                return ENOMEM;
     77
    7378        prg->loader_status = EE_OK;
    7479        prg->task = task_create(as, name);
    75         if (!prg->task)
     80        if (!prg->task) {
     81                free(kernel_uarg);
    7682                return ELIMIT;
     83        }
    7784
    7885        /*
     
    9097            &anon_backend, NULL, &virt, bound);
    9198        if (!area) {
     99                free(kernel_uarg);
    92100                task_destroy(prg->task);
    93101                prg->task = NULL;
    94102                return ENOMEM;
    95103        }
    96 
    97         uspace_arg_t *kernel_uarg = (uspace_arg_t *)
    98             malloc(sizeof(uspace_arg_t), 0);
    99104
    100105        kernel_uarg->uspace_entry = (void *) entry_addr;
  • kernel/generic/src/proc/thread.c

    rae89656 r7473807  
    949949         */
    950950        uspace_arg_t *kernel_uarg =
    951             (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
     951            (uspace_arg_t *) malloc(sizeof(uspace_arg_t), FRAME_ATOMIC);
     952        if (!kernel_uarg)
     953                return (sys_errno_t) ENOMEM;
    952954
    953955        rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
  • kernel/generic/src/synch/futex.c

    rae89656 r7473807  
    339339static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
    340340{
    341         futex_t *futex = malloc(sizeof(futex_t), 0);
     341        futex_t *futex = malloc(sizeof(futex_t), FRAME_ATOMIC);
     342        if (!futex)
     343                return NULL;
    342344
    343345        /*
     
    363365         * Cache the link to the futex object for this task.
    364366         */
    365         futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
     367        futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), FRAME_ATOMIC);
     368        if (!fut_ptr) {
     369                spinlock_lock(&futex_ht_lock);
     370                futex_release_ref(futex);
     371                spinlock_unlock(&futex_ht_lock);
     372                return NULL;
     373        }
    366374        cht_link_t *dup_link;
    367375
  • kernel/generic/src/synch/workqueue.c

    rae89656 r7473807  
    187187struct work_queue *workq_create(const char *name)
    188188{
    189         struct work_queue *workq = malloc(sizeof(struct work_queue), 0);
     189        struct work_queue *workq = malloc(sizeof(struct work_queue),
     190            FRAME_ATOMIC);
     191        if (!workq)
     192                return NULL;
    190193
    191194        if (workq) {
  • kernel/generic/src/udebug/udebug_ops.c

    rae89656 r7473807  
    370370
    371371        /* Allocate a buffer to hold thread IDs */
    372         sysarg_t *id_buffer = malloc(buf_size + 1, 0);
     372        sysarg_t *id_buffer = malloc(buf_size + 1, FRAME_ATOMIC);
     373        if (!id_buffer)
     374                return ENOMEM;
    373375
    374376        mutex_lock(&TASK->udebug.lock);
     
    432434        size_t name_size = str_size(TASK->name) + 1;
    433435
    434         *data = malloc(name_size, 0);
     436        *data = malloc(name_size, FRAME_ATOMIC);
     437        if (!*data)
     438                return ENOMEM;
    435439        *data_size = name_size;
    436440
     
    472476
    473477        /* Prepare a buffer to hold the arguments. */
    474         sysarg_t *arg_buffer = malloc(6 * sizeof(sysarg_t), 0);
     478        sysarg_t *arg_buffer = malloc(6 * sizeof(sysarg_t), FRAME_ATOMIC);
     479        if (!arg_buffer) {
     480                _thread_op_end(thread);
     481                return ENOMEM;
     482        }
    475483
    476484        /* Copy to a local buffer before releasing the lock. */
     
    514522
    515523        /* Prepare a buffer to hold the data. */
    516         istate_t *state_buf = malloc(sizeof(istate_t), 0);
     524        istate_t *state_buf = malloc(sizeof(istate_t), FRAME_ATOMIC);
     525        if (!state_buf) {
     526                _thread_op_end(thread);
     527                return ENOMEM;
     528        }
    517529
    518530        /* Copy to the allocated buffer */
     
    546558        }
    547559
    548         void *data_buffer = malloc(n, 0);
     560        void *data_buffer = malloc(n, FRAME_ATOMIC);
     561        if (!data_buffer) {
     562                mutex_unlock(&TASK->udebug.lock);
     563                return ENOMEM;
     564        }
    549565
    550566        /*
  • kernel/test/mm/falloc1.c

    rae89656 r7473807  
    4747
    4848        uintptr_t *frames = (uintptr_t *)
    49             malloc(MAX_FRAMES * sizeof(uintptr_t), 0);
     49            malloc(MAX_FRAMES * sizeof(uintptr_t), FRAME_ATOMIC);
    5050        if (frames == NULL)
    5151                return "Unable to allocate frames";
  • uspace/lib/c/include/rcu.h

    rae89656 r7473807  
    5555 *
    5656 * // Insert at the beginning of the list.
    57  * exam_t *my_exam = malloc(sizeof(exam_t), 0);
     57 * exam_t *my_exam = malloc(sizeof(exam_t));
    5858 * my_exam->grade = 5;
    5959 * my_exam->next = exam_list;
Note: See TracChangeset for help on using the changeset viewer.