Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 2498b95 in mainline


Ignore:
Timestamp:
2018-06-25T15:49:26Z (2 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master
Children:
fbfe59d
Parents:
bfe90b6
git-author:
Jiri Svoboda <jiri@…> (2018-06-24 19:48:37)
git-committer:
Jiri Svoboda <jiri@…> (2018-06-25 15:49:26)
Message:

Move memchr to libc and add tests for other memxxx functions.

Location:
uspace/lib
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/bithenge/src/helenos/common.h

    rbfe90b6 r2498b95  
    7373}
    7474
    75 static inline void *memchr(const void *s, int c, size_t n)
    76 {
    77         for (size_t i = 0; i < n; i++)
    78                 if (((char *)s)[i] == c)
    79                         return (void *)(s + i);
    80         return NULL;
    81 }
    82 
    8375static inline errno_t bithenge_parse_int(const char *start, bithenge_int_t *result)
    8476{
  • uspace/lib/c/Makefile

    rbfe90b6 r2498b95  
    193193        test/fibril/timer.c \
    194194        test/main.c \
     195        test/mem.c \
    195196        test/io/table.c \
    196197        test/stdio/scanf.c \
  • uspace/lib/c/generic/mem.c

    rbfe90b6 r2498b95  
    11/*
    22 * Copyright (c) 2005 Martin Decky
    3  * Copyright (c) 2008 Jiri Svoboda
     3 * Copyright (c) 2018 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    252252}
    253253
     254/** Search memory area.
     255 *
     256 * @param s Memory area
     257 * @param c Character (byte) to search for
     258 * @param n Size of memory area in bytes
     259 *
     260 * @return Pointer to the first occurrence of @a c in the first @a n
     261 *         bytes of @a s or @c NULL if not found.
     262 */
     263void *memchr(const void *s, int c, size_t n)
     264{
     265        uint8_t *u = (uint8_t *) s;
     266        unsigned char uc = (unsigned char) c;
     267        size_t i;
     268
     269        for (i = 0; i < n; i++) {
     270                if (u[i] == uc)
     271                        return (void *) &u[i];
     272        }
     273
     274        return NULL;
     275}
     276
    254277/** @}
    255278 */
  • uspace/lib/c/include/mem.h

    rbfe90b6 r2498b95  
    11/*
    22 * Copyright (c) 2005 Martin Decky
     3 * Copyright (c) 2018 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    4950extern int memcmp(const void *, const void *, size_t)
    5051    __attribute__((nonnull(1, 2)));
     52extern void *memchr(const void *, int, size_t)
     53    __attribute__((nonnull(1)));
    5154
    5255#endif
  • uspace/lib/c/test/main.c

    rbfe90b6 r2498b95  
    3434PCUT_IMPORT(circ_buf);
    3535PCUT_IMPORT(fibril_timer);
     36PCUT_IMPORT(mem);
    3637PCUT_IMPORT(odict);
    3738PCUT_IMPORT(qsort);
  • uspace/lib/posix/include/posix/string.h

    rbfe90b6 r2498b95  
    3636#ifndef POSIX_STRING_H_
    3737#define POSIX_STRING_H_
    38 
    39 #include "sys/types.h"
    40 
    4138/*
    4239 * TODO: not implemented due to missing locale support
     
    4845
    4946#include <_bits/NULL.h>
     47#include <_bits/size_t.h>
    5048
    51 /*
    52  * These are the same as in HelenOS libc.
    53  * It would be possible to directly include <str.h> and <mem.h> but
    54  * it is better not to pollute POSIX namespace with other functions
    55  * defined in that header.
    56  *
    57  * Because libposix is always linked with libc, providing only these
    58  * forward declarations ought to be enough.
    59  */
    60 
    61 /* From mem.h */
    62 // #define bzero(ptr, len)  memset((ptr), 0, (len))
    63 extern void *memset(void *, int, size_t)
    64     __attribute__((nonnull(1)));
    65 extern void *memcpy(void *, const void *, size_t)
    66     __attribute__((nonnull(1, 2)));
    67 extern void *memmove(void *, const void *, size_t)
    68     __attribute__((nonnull(1, 2)));
    69 
     49#include "libc/mem.h"
    7050
    7151/* Copying and Concatenation */
     
    8060extern char *strndup(const char *s, size_t n);
    8161
    82 /* String/Array Comparison */
    83 extern int memcmp(const void *mem1, const void *mem2, size_t n);
     62/* String Comparison */
    8463extern int strcmp(const char *s1, const char *s2);
    8564extern int strncmp(const char *s1, const char *s2, size_t n);
    8665
    8766/* Search Functions */
    88 extern void *memchr(const void *mem, int c, size_t n);
    8967extern char *strchr(const char *s, int c);
    9068extern char *strrchr(const char *s, int c);
     
    122100#endif
    123101
    124 
    125102#endif  // POSIX_STRING_H_
    126103
  • uspace/lib/posix/src/string.c

    rbfe90b6 r2498b95  
    288288
    289289        return 0;
    290 }
    291 
    292 /**
    293  * Find byte in memory.
    294  *
    295  * @param mem Memory area in which to look for the byte.
    296  * @param c Byte to look for.
    297  * @param n Maximum number of bytes to be inspected.
    298  * @return Pointer to the specified byte on success,
    299  *     NULL pointer otherwise.
    300  */
    301 void *memchr(const void *mem, int c, size_t n)
    302 {
    303         assert(mem != NULL);
    304 
    305         const unsigned char *s = mem;
    306 
    307         for (size_t i = 0; i < n; ++i) {
    308                 if (s[i] == (unsigned char) c) {
    309                         return (void *) &s[i];
    310                 }
    311         }
    312         return NULL;
    313290}
    314291
Note: See TracChangeset for help on using the changeset viewer.