source: trunk/kernel/libk/memset.c @ 1

Last change on this file since 1 was 1, checked in by alain, 7 years ago

First import

File size: 2.1 KB
Line 
1/*
2 * memset.c - architecture independent memory set function
3 *
4 * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
5 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
6 *
7 * This file is part of ALMOS-kernel.
8 *
9 * ALMOS-kernel is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-kernel is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <string.h>
24
25/* TODO: introduce the possiblity to use architecture specific
26 * more optimized version if any */
27
28void do_memset_8(void *dst, uint32_t val, size_t size)
29{
30        volatile uint32_t *dst_ptr;
31        register uint32_t *limit_ptr;
32 
33        dst_ptr   = dst;
34        limit_ptr = (uint32_t*)((uint8_t*)dst + (size << 5));
35 
36        while(dst_ptr != limit_ptr)
37        {
38                dst_ptr[0] = val;
39                dst_ptr[1] = val;
40                dst_ptr[2] = val;
41                dst_ptr[3] = val;
42                dst_ptr[4] = val;
43                dst_ptr[5] = val;
44                dst_ptr[6] = val;
45                dst_ptr[7] = val;
46
47                dst_ptr += 8;
48        }
49}
50
51#include <kdmsg.h>
52#include <cpu.h>
53void *memset(void *s, int c, unsigned int size)
54{
55        register uint8_t  *ptr = s;
56        register uint32_t *wptr;
57        register uint32_t val;
58        register uint32_t isize;
59        register uint32_t count;
60 
61        c&=0xFF;
62        ptr = s;
63
64        while(((uint32_t) ptr & 0x3) && size && size--)
65                *(ptr++) = (uint8_t) c;
66
67        if(size == 0) return s;
68
69        val = c << 24 | c << 16 | c << 8 | (c & 0xFF);
70        isize = size >> 5;
71
72        if(isize != 0)
73                do_memset_8(ptr, val, isize);
74
75        count = isize << 5;
76        size -= count;
77 
78        if(size == 0) return s;
79
80        wptr = (uint32_t*)(ptr + count);
81        isize = size >> 2;
82        count = isize;
83
84        while(isize--)
85                *(wptr++) = val;
86
87        size = size - (count << 2);
88        ptr = (uint8_t*)wptr;
89
90        while(size--)
91                *(ptr++) = (uint8_t)c;
92 
93        return s;
94}
Note: See TracBrowser for help on using the repository browser.