From patchwork Thu Jun 8 21:13:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103412 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632641qgd; Thu, 8 Jun 2017 14:15:23 -0700 (PDT) X-Received: by 10.84.253.23 with SMTP id z23mr37160411pll.11.1496956523729; Thu, 08 Jun 2017 14:15:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956523; cv=none; d=google.com; s=arc-20160816; b=nSY02A1OSNAVHZl4mX7FG9oAGbcVWkjsCQwQByTMgeR62oRnvyXg7uNAyc4bQEmhxQ HGks5IRNr7JkkmtSbjRFPw+gA2AHRU5fiDksMTn5aPPcl+f6Zdo6QNhC5aNyiIPQ0Hse 4F87cOPcxsf/xrJA9esUXrwzvxW493eobr7h0lu2jz+KhU8VuYtuuXHtGD+P5oYUSAmM DPb43LtDrL+cn1QSnaUhYUQuUSZ0fWjb2WI/hQuwQp3A+T3NHQRYfoKlhJkSgmau1YCT ORkkxkT568W4k2QTdKD3sxqekQF9fpNuvOk80JpKcHSd3SymZhKOMQuVpnFo0NPEAUXG SojQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=zvQtk2LxFfAa/A9xrzK/27Oe92vcRC2B+RfjFRVE4+Q=; b=ndWuzu4FV3dC2/2985kjz2w0WV+JAp1iZWN0EqewWh3arB5SEKBP9zgKvMvm4cc18z 2RyOPD1qVCaXx8O9SWonaPVeFRnkEx68vH/HfaG6LFqp5Zmb+EdE720mOkRJjKXc5yx+ h3Dmp7hyG8z2BflLEAFVPLw3OjzS1Ca+ssbHI/XKkFbr8iCkctmTvRxHNAVq7q0jIA4n IolGEMt/7rS5p6iJM9tHinMCjqI+FXYZc3jnLnGREhqiV8P15L09zbSQ5PmO02B9Kz9X lYG6sxBmAs+iBxILTGPcBAu4oGANP+ltqaNwvvtNcUTMtRPm21iGyQdQIPgbh7QW4x55 b/rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80206-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id j15si5219081pga.273.2017.06.08.14.15.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:23 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80206-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=MDFLTIRgRfi97LmmdjCEaFBwo7xga3x 4ftE2PY0e0MAhr9bmsRZlzi98LqjJu0i9pNFfUIhK6FYqRcWhRpbtfxKFNGttTHb m4LJzWtv721c3xseQrpy5k/aaJfyAIOJTlSb+8uBC34OVCi90VWvDWLzv7qSNUCH G4UqUQ4Ro3RI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=tAz8zKsS6JfIV2VbPHUjyvTWW1Q=; b=jmQz9 6wh6hbn2lJXGmVV4jSNBHBxClrQcmrWxc5xoWHVeafDg0fLlbksutfq2SLwqsnl7 vYZlYwwdve0R2zSIt1Wnl1aWG7MS69zzNaEzhZ3BSBDLTeTLOFk1vSo7+27alzD8 lG/e0isZ1pcBUSMALc6uBTb5EX73PwfHM5lD84= Received: (qmail 39150 invoked by alias); 8 Jun 2017 21:14:04 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38967 invoked by uid 89); 8 Jun 2017 21:14:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=maintaining, 1014 X-HELO: mail-qt0-f176.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=zvQtk2LxFfAa/A9xrzK/27Oe92vcRC2B+RfjFRVE4+Q=; b=GML72pi3SPkQMYFtpb8FepYJQzrpaowYoRTzrfPNpXh8/aRbAm1LL6s3XDTbJzTT/J +l34Vl3nvETNwFXlWcj86YHYW9BnuXZIXBxYcAu2Saw+nnhxuqMINj7oebX/iPz+UasV f97ZznYi9OPGiiFotlfdioMrRvRsRsXwwBN1V0NcRjuFGsT4pbcmjTh3SArGYgoFk9iS hkJl5rJnOHh+GPXm5L6wddm2W4J/yjfJAZe2OjH0OV6eZl/H55uLa4TiRL0MpGs8vk+t VrRvl3jRKsVyGj7ds9+bKeU254Zog268gynKyQiDGn7RSusd2eB3Xg9oci5blftCu9cL lkjw== X-Gm-Message-State: AODbwcBSyMST7crFZQR9ePeQ0sSH48MHZobDKMrKCU4VjwakQ269Y8Sh Ez4CXDbRpl7kKRSgHZKW2Q== X-Received: by 10.237.62.8 with SMTP id l8mr46309110qtf.20.1496956439994; Thu, 08 Jun 2017 14:13:59 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 07/17] malloc: Add specialized dynarray for C strings Date: Thu, 8 Jun 2017 18:13:21 -0300 Message-Id: <1496956411-25594-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds an specialized dynarray to manage C strings using the dynarray internal implementation. It uses some private fields from dynarray and thus it provided specific files to access and manage the internal string buffer. For instance: struct char_array str; // str == "testing" char_array_init_str (&str, "testing"); // c == 's' char c = char_array_pos (&str, 2); // str = "testing2" char_array_set_str (&str, "testing2"); // str = "testi" char_array_erase (&str, 5); // str = "123testi" char_array_prepend_str (&str, "123"); // len = 8 size_t len = char_array_length (&str); // str = "123testi456" char_array_append_str (&str, "456"); // str = "123testi789" char_array_replace_str_pos (&str, 7, "789", 3); The provided function are not extensive and meant mainly to be use in subsequent glob implementation cleanup. For internal object consistency only the function provided by char_array.c should be used, including internal object manipulation. To check for possible overflows in internal size manipulation a new function, check_add_wrapv_size_t, is added on malloc-internal. It basically return whether the addition of two size_t overflows. Checked on x86_64-linux-gnu. * malloc/Makefile (test-internal): Add tst-char_array. * malloc/malloc-internal (check_add_wrapv_size_t): New function. * malloc/char_array.c: New file. * malloc/tst-char-array.c: Likewise. --- malloc/Makefile | 1 + malloc/char_array.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++ malloc/malloc-internal.h | 14 +++ malloc/tst-char_array.c | 107 ++++++++++++++++++++ 4 files changed, 378 insertions(+) create mode 100644 malloc/char_array.c create mode 100644 malloc/tst-char_array.c -- 2.7.4 diff --git a/malloc/Makefile b/malloc/Makefile index af025cb..098e3c6 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -46,6 +46,7 @@ tests-internal += \ tst-dynarray \ tst-dynarray-fail \ tst-dynarray-at-fail \ + tst-char_array ifneq (no,$(have-tunables)) tests += tst-malloc-usable-tunables diff --git a/malloc/char_array.c b/malloc/char_array.c new file mode 100644 index 0000000..cce9360 --- /dev/null +++ b/malloc/char_array.c @@ -0,0 +1,256 @@ +/* Specialized dynarray for C strings. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* This file provides a dynamic C string with an initial stack allocated + buffer. Since it is based on dynarray, it provided dynamic size + expansion and heap usage for large strings. + + The following parameters are optional: + + CHAR_ARRAY_INITIAL_SIZE + The size of the statically allocated array (default is 256). It will + be used to define DYNARRAY_INITIAL_SIZE. + + The following functions are provided: + + bool char_array_init_empty (struct char_array *); + bool char_array_init_str (struct char_array *, const char *); + bool char_array_init_str_size (struct char_array *, const char *, size_t); + bool char_array_is_empty (struct char_array *); + const char *char_array_str (struct char_array *); + char char_array_pos (struct char_array *, size_t); + size_t char_array_length (struct char_array *); + bool char_array_set_str (struct char_array *, const char *); + bool char_array_set_str_size (struct char_array *, const char *, size_t); + void char_array_erase (struct char_array *, size_t); + bool char_array_resize_str (struct char_array *, size_t); + bool char_array_prepend_str (struct char_array *, const char *); + bool char_array_append_str (struct char_array *, const char *); + bool char_array_replace_str_pos (struct char_array *, size_t, const char *, + size_t); + + For instance: + + struct char_array str; + // str == "testing"; + char_array_init_str (&str, "testing"); + // c == 's' + char c = char_array_pos (&str, 2); + // str = "testing2"; + char_array_set_str (&str, "testing2"); + // str = "testi"; + char_array_erase (&str, 5); + // str = "123testi"; + char_array_prepend_str (&str, "123"); + // len = 8; + size_t len = char_array_length (&str); + // str = "123testi456"; + char_array_append_str (&str, "456"); + // str = "123testi789"; + char_array_replace_str_pos (&str, 7, "789", 3); + */ + +#define DYNARRAY_STRUCT char_array +#define DYNARRAY_ELEMENT char +#define DYNARRAY_PREFIX char_array_ +#define DYNARRAY_ELEMENT_INIT(__e) (*__e = '\0') +#ifndef CHAR_ARRAY_INITIAL_SIZE +# define CHAR_ARRAY_INITIAL_SIZE 256 +#endif +#define DYNARRAY_INITIAL_SIZE CHAR_ARRAY_INITIAL_SIZE +#include +#include + +/* Return a const char for the internal C string handled by 'array'. */ +static const char * __attribute_used__ +char_array_str (struct char_array *array) +{ + return char_array_at (array, 0); +} + +/* Return the character at position 'pos' from the char_array 'array'. */ +static char __attribute_used__ +char_array_pos (struct char_array *array, size_t pos) +{ + return *char_array_at (array, pos); +} + +static size_t __attribute_used__ +char_array_length (struct char_array *array) +{ + /* Exclude the final '\0'. */ + return array->dynarray_header.used - 1; +} + +/* Copy the contents of string 'str' to char_array 'array', including the + final '\0'. */ +static bool __attribute_used__ +char_array_set_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str) + 1; + if (!char_array_resize (array, size)) + return false; + memcpy (array->dynarray_header.array, str, size); + array->dynarray_header.used = size; + return true; +} + +/* Copy up 'size' bytes from string 'str' to char_array 'array'. A final + '\0' is appended in the char_array. */ +static bool __attribute_used__ +char_array_set_str_size (struct char_array *array, const char *str, + size_t size) +{ + size_t newsize; + if (check_add_wrapv_size_t (size, 1, &newsize) + || !char_array_resize (array, newsize)) + return false; + *((char *) mempcpy (array->dynarray_header.array, str, size)) = '\0'; + array->dynarray_header.used = newsize; + return true; +} + +/* Initialize the char_array 'array' and sets it to an empty string (""). */ +static bool __attribute_used__ +char_array_init_empty (struct char_array *array) +{ + char_array_init (array); + return char_array_set_str (array, ""); +} + +/* Initialize the char_array 'array' and copy the content of string 'str'. */ +static bool __attribute_used__ +char_array_init_str (struct char_array *array, const char *str) +{ + char_array_init (array); + return char_array_set_str (array, str); +} + +/* Initialize the char_array 'array' and copy the content of string 'str' + up to 'size' characteres. */ +static bool __attribute_used__ +char_array_init_str_size (struct char_array *array, const char *str, + size_t size) +{ + char_array_init (array); + return char_array_set_str_size (array, str, size); +} + +static bool __attribute_used__ +char_array_is_empty (struct char_array *array) +{ + return *char_array_at (array, 0) == '\0'; +} + +/* Remove the byte at position 'pos' from char_array 'array'. The contents + are moved internally if the position is not at the end of the internal + buffer. */ +static bool __attribute_used__ +char_array_erase (struct char_array *array, size_t pos) +{ + if (pos >= array->dynarray_header.used - 1) + return false; + + char *ppos = char_array_at (array, pos); + char *lpos = array->dynarray_header.array + array->dynarray_header.used; + ptrdiff_t size = lpos - ppos; + memmove (ppos, ppos + 1, size); + array->dynarray_header.used--; + return true; +} + +/* Resize the char_array 'array' to size 'count' maintaining the ending + '\0' byte. */ +static bool __attribute_used__ +char_array_crop (struct char_array *array, size_t size) +{ + if (size >= (array->dynarray_header.used - 1) + || !char_array_resize (array, size + 1)) + return false; + + array->dynarray_header.array[array->dynarray_header.used] = '\0'; + return true; +} + +/* Prepend the contents of string 'str' to char_array 'array', including the + final '\0' byte. */ +static bool __attribute_used__ +char_array_prepend_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str); + /* Resizing the array might change its used elements and we need below + to correct copy the elements. */ + size_t used = array->dynarray_header.used; + + size_t newsize; + if (check_add_wrapv_size_t (used, size, &newsize) + || !char_array_resize (array, newsize)) + return false; + + /* Make room for the string and copy it. */ + memmove (array->dynarray_header.array + size, array->dynarray_header.array, + used); + memcpy (array->dynarray_header.array, str, size); + array->dynarray_header.used = newsize; + return true; +} + +/* Append the contents of string 'str' to char_array 'array, including the + final '\0' byte. */ +static bool __attribute_used__ +char_array_append_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str); + /* Resizing the array might change its used elements and it used it below + to correct copy the elements. */ + size_t used = array->dynarray_header.used - 1; + + /* array 'used' does account for final '\0', so there is no need to add + an extra element to calculate the final required size. */ + size_t newsize; + if (check_add_wrapv_size_t (used + 1, size, &newsize) + || !char_array_resize (array, newsize)) + return false; + + /* Start to append at '\0' up to string length and add a final '\0'. */ + *(char*) mempcpy (array->dynarray_header.array + used, str, size) = '\0'; + array->dynarray_header.used = newsize; + return true; +} + +/* Replace the contents starting of position 'pos' of char_array 'array' + with the contents of string 'str' up to 'len' bytes. A final '\0' + is appended in the string. */ +static bool __attribute_used__ +char_array_replace_str_pos (struct char_array *array, size_t pos, + const char *str, size_t len) +{ + if (pos > array->dynarray_header.used) + return false; + + size_t newsize; + if (check_add_wrapv_size_t (pos, len, &newsize) + || check_add_wrapv_size_t (newsize, 1, &newsize) + || !char_array_resize (array, newsize)) + return false; + + char *start = char_array_at (array, pos); + *(char *) mempcpy (start, str, len) = '\0'; + array->dynarray_header.used = newsize; + return true; +} diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h index dbd801a..3066cd3 100644 --- a/malloc/malloc-internal.h +++ b/malloc/malloc-internal.h @@ -101,4 +101,18 @@ check_mul_overflow_size_t (size_t left, size_t right, size_t *result) #endif } +/* Set *R = A + B. Return true if the answer is mathematically incorrect due + to overflow; in this case, *R is the low order bits of the correct + answer. */ +static inline bool +check_add_wrapv_size_t (size_t a, size_t b, size_t *r) +{ +#if 5 <= __GNUC__ + return __builtin_add_overflow (a, b, r); +#else + *r = a + b; + return *r < a; +#endif +} + #endif /* _MALLOC_INTERNAL_H */ diff --git a/malloc/tst-char_array.c b/malloc/tst-char_array.c new file mode 100644 index 0000000..53f9482 --- /dev/null +++ b/malloc/tst-char_array.c @@ -0,0 +1,107 @@ +/* Test for char_array. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + mtrace (); + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_empty (&str) == true); + TEST_VERIFY_EXIT (char_array_length (&str) == 0); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == true); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing")); + TEST_VERIFY_EXIT (char_array_pos (&str, 2) == 's'); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == false); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testing") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str_size (&str, "testing", 4)); + TEST_VERIFY_EXIT (char_array_length (&str) == 4); + TEST_VERIFY_EXIT (char_array_pos (&str, 2) == 's'); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == false); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "test") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_set_str (&str, "abcdef")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "abcdef") == 0); + TEST_VERIFY_EXIT (char_array_set_str_size (&str, "abcdef", 4)); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "abcd") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_erase (&str, 4) == true); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 1); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testng") == 0); + TEST_VERIFY_EXIT (char_array_erase (&str, char_array_length (&str)) + == false); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 1); + TEST_VERIFY_EXIT (char_array_erase (&str, char_array_length (&str) - 1) + == true); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 2); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testn") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "test")); + TEST_VERIFY_EXIT (char_array_prepend_str (&str, "123")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test")); + TEST_VERIFY_EXIT (char_array_append_str (&str, "456")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test456") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test456")); + TEST_VERIFY_EXIT (char_array_replace_str_pos (&str, 7, "789", 3)); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test789") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test789")); + char_array_free (&str); + } + + return 0; +} + +#include