From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=PDS_BAD_THREAD_QP_64, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by gnuweeb.org (Postfix) with ESMTPS id 59C647E308 for ; Tue, 22 Mar 2022 11:52:46 +0000 (UTC) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-401-rM2dEyRPOySuFi2zcClhJw-1; Tue, 22 Mar 2022 11:52:43 +0000 X-MC-Unique: rM2dEyRPOySuFi2zcClhJw-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Tue, 22 Mar 2022 11:52:43 +0000 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.033; Tue, 22 Mar 2022 11:52:43 +0000 From: David Laight To: 'Ammar Faizi' , Willy Tarreau CC: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , "Linux Kernel Mailing List" , GNU/Weeb Mailing List Subject: RE: [RFC PATCH v2 6/8] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` Thread-Topic: [RFC PATCH v2 6/8] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` Thread-Index: AQHYPdaybn74E+3FG0GW9mJht1aJxazLSY+g Date: Tue, 22 Mar 2022 11:52:43 +0000 Message-ID: <56935393241242adab6f32c50dd74c23@AcuMS.aculab.com> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> <20220322102115.186179-7-ammarfaizi2@gnuweeb.org> In-Reply-To: <20220322102115.186179-7-ammarfaizi2@gnuweeb.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: From: Ammar Faizi > Sent: 22 March 2022 10:21 >=20 > Implement basic dynamic allocator functions. These functions are > currently only available on architectures that have nolibc mmap() > syscall implemented. These are not a super-fast memory allocator, > but at least they can satisfy basic needs for having heap without > libc. >=20 > Signed-off-by: Ammar Faizi > --- >=20 > @@ Changelog: >=20 > Link: https://lore.kernel.org/lkml/20220320093750.159991-6-ammarfaizi2= @gnuweeb.org > RFC v1 -> RFC v2: > - Move container_of() and offsetof() macro to types.h with a > separate patch (comment from Willy). > --- > tools/include/nolibc/stdlib.h | 68 +++++++++++++++++++++++++++++++++++ > 1 file changed, 68 insertions(+) >=20 > diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.= h > index aca8616335e3..a0ed75431e0a 100644 > --- a/tools/include/nolibc/stdlib.h > +++ b/tools/include/nolibc/stdlib.h > @@ -11,7 +11,12 @@ > #include "arch.h" > #include "types.h" > #include "sys.h" > +#include "string.h" >=20 > +struct nolibc_heap { > +=09size_t=09len; > +=09char=09user_p[] __attribute__((__aligned__)); Doesn't that need (number) in the attribute? > +}; >=20 > /* Buffer used to store int-to-ASCII conversions. Will only be implement= ed if > * any of the related functions is implemented. The area is large enough= to > @@ -60,6 +65,18 @@ int atoi(const char *s) > =09return atol(s); > } >=20 > +static __attribute__((unused)) > +void free(void *ptr) > +{ > +=09struct nolibc_heap *heap; > + > +=09if (!ptr) > +=09=09return; > + > +=09heap =3D container_of(ptr, struct nolibc_heap, user_p); > +=09munmap(heap, heap->len); > +} > + > /* Tries to find the environment variable named in the environmen= t array > * pointed to by global variable "environ" which must be declared as a c= har **, > * and must be terminated by a NULL (it is recommended to set this varia= ble to > @@ -205,6 +222,57 @@ char *ltoa(long in) > =09return itoa_buffer; > } >=20 > +static __attribute__((unused)) > +void *malloc(size_t len) > +{ > +=09struct nolibc_heap *heap; If you do (say): =09len =3D ROUNDUP(len + sizeof *heap, 4096) you can optimise a lot of the realloc() calls. I actually wonder if compiling a mini-libc.a and then linking the programs against it might be better than all these static functions? -ffunction-sections can help a bit (where supported). =09David =09 > + > +=09heap =3D mmap(NULL, sizeof(*heap) + len, PROT_READ|PROT_WRITE, > +=09=09 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); > +=09if (__builtin_expect(heap =3D=3D MAP_FAILED, 0)) > +=09=09return NULL; > + > +=09heap->len =3D sizeof(*heap) + len; > +=09return heap->user_p; > +} > + > +static __attribute__((unused)) > +void *calloc(size_t size, size_t nmemb) > +{ > +=09void *orig; > +=09size_t res =3D 0; > + > +=09if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) { > +=09=09SET_ERRNO(ENOMEM); > +=09=09return NULL; > +=09} > + > +=09/* > +=09 * No need to zero the heap, the MAP_ANONYMOUS in malloc() > +=09 * already does it. > +=09 */ > +=09return malloc(res); > +} > + > +static __attribute__((unused)) > +void *realloc(void *old_ptr, size_t new_size) > +{ > +=09struct nolibc_heap *heap; > +=09void *ret; > + > +=09ret =3D malloc(new_size); > +=09if (__builtin_expect(!ret, 0)) > +=09=09return NULL; > + > +=09if (!old_ptr) > +=09=09return ret; > + > +=09heap =3D container_of(old_ptr, struct nolibc_heap, user_p); > +=09memcpy(ret, heap->user_p, heap->len); > +=09munmap(heap, heap->len); > +=09return ret; > +} > + > /* converts unsigned long integer to a string using the static itoa= _buffer > * and returns the pointer to that string. > */ > -- > Ammar Faizi - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)