9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] make arrays big: qsort edition
@ 2021-07-29 17:14 ori
  2021-07-29 23:15 ` Nick Owens
  0 siblings, 1 reply; 6+ messages in thread
From: ori @ 2021-07-29 17:14 UTC (permalink / raw)
  To: 9front

Since usize exists, and is now 64 bit, we
should start slowly going through our code
and finding the places where we're using
int, short, etc in unexpected ways, so
that we can start allowing big arrays.

In some cases -- function parameters,
mainly, this is just a matter of switching
the types.

In others, like strlen, we may need to go
through all possible callers and make sure
that a new, bigger, return type doesn't get
silently truncated and cause issues.

This change does qsort.

--- /dist/plan9front/.git/fs/object/84b77568cda98a21dd4f3403a94c56724fbd5b37/tree//sys/include/libc.h
+++ /sys/include/libc.h
@@ -424,7 +424,7 @@
 extern	int	postnote(int, int, char *);
 extern	double	pow10(int);
 extern	int	putenv(char*, char*);
-extern	void	qsort(void*, long, long, int (*)(void*, void*));
+extern	void	qsort(void*, usize, usize, int (*)(void*, void*));
 extern	int	setjmp(jmp_buf);
 extern	double	strtod(char*, char**);
 extern	long	strtol(char*, char**, int);
--- /dist/plan9front/.git/fs/object/84b77568cda98a21dd4f3403a94c56724fbd5b37/tree//sys/src/libc/port/qsort.c
+++ /sys/src/libc/port/qsort.c
@@ -8,12 +8,12 @@
 struct
 {
 	int	(*cmp)(void*, void*);
-	void	(*swap)(char*, char*, long);
-	long	es;
+	void	(*swap)(char*, char*, usize);
+	usize	es;
 } Sort;
 
 static	void
-swapb(char *i, char *j, long es)
+swapb(char *i, char *j, usize es)
 {
 	char c;
 
@@ -27,7 +27,7 @@
 }
 
 static	void
-swapi(char *ii, char *ij, long es)
+swapi(char *ii, char *ij, usize es)
 {
 	long *i, *j, c;
 
@@ -42,9 +42,9 @@
 }
 
 static	char*
-pivot(char *a, long n, Sort *p)
+pivot(char *a, usize n, Sort *p)
 {
-	long j;
+	usize j;
 	char *pi, *pj, *pk;
 
 	j = n/6 * p->es;
@@ -69,9 +69,9 @@
 }
 
 static	void
-qsorts(char *a, long n, Sort *p)
+qsorts(char *a, usize n, Sort *p)
 {
-	long j, es;
+	usize j, es;
 	char *pi, *pj, *pn;
 
 	es = p->es;
@@ -111,7 +111,7 @@
 }
 
 void
-qsort(void *va, long n, long es, int (*cmp)(void*, void*))
+qsort(void *va, usize n, usize es, int (*cmp)(void*, void*))
 {
 	Sort s;
 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] make arrays big: qsort edition
  2021-07-29 17:14 [9front] make arrays big: qsort edition ori
@ 2021-07-29 23:15 ` Nick Owens
  2021-07-30 17:24   ` Steve Simon
  2021-08-01 23:53   ` ori
  0 siblings, 2 replies; 6+ messages in thread
From: Nick Owens @ 2021-07-29 23:15 UTC (permalink / raw)
  To: 9front

are you able to test this change with a very large array?

On Thu, Jul 29, 2021 at 10:17 AM <ori@eigenstate.org> wrote:
>
> Since usize exists, and is now 64 bit, we
> should start slowly going through our code
> and finding the places where we're using
> int, short, etc in unexpected ways, so
> that we can start allowing big arrays.
>
> In some cases -- function parameters,
> mainly, this is just a matter of switching
> the types.
>
> In others, like strlen, we may need to go
> through all possible callers and make sure
> that a new, bigger, return type doesn't get
> silently truncated and cause issues.
>
> This change does qsort.
>
> --- /dist/plan9front/.git/fs/object/84b77568cda98a21dd4f3403a94c56724fbd5b37/tree//sys/include/libc.h
> +++ /sys/include/libc.h
> @@ -424,7 +424,7 @@
>  extern int     postnote(int, int, char *);
>  extern double  pow10(int);
>  extern int     putenv(char*, char*);
> -extern void    qsort(void*, long, long, int (*)(void*, void*));
> +extern void    qsort(void*, usize, usize, int (*)(void*, void*));
>  extern int     setjmp(jmp_buf);
>  extern double  strtod(char*, char**);
>  extern long    strtol(char*, char**, int);
> --- /dist/plan9front/.git/fs/object/84b77568cda98a21dd4f3403a94c56724fbd5b37/tree//sys/src/libc/port/qsort.c
> +++ /sys/src/libc/port/qsort.c
> @@ -8,12 +8,12 @@
>  struct
>  {
>         int     (*cmp)(void*, void*);
> -       void    (*swap)(char*, char*, long);
> -       long    es;
> +       void    (*swap)(char*, char*, usize);
> +       usize   es;
>  } Sort;
>
>  static void
> -swapb(char *i, char *j, long es)
> +swapb(char *i, char *j, usize es)
>  {
>         char c;
>
> @@ -27,7 +27,7 @@
>  }
>
>  static void
> -swapi(char *ii, char *ij, long es)
> +swapi(char *ii, char *ij, usize es)
>  {
>         long *i, *j, c;
>
> @@ -42,9 +42,9 @@
>  }
>
>  static char*
> -pivot(char *a, long n, Sort *p)
> +pivot(char *a, usize n, Sort *p)
>  {
> -       long j;
> +       usize j;
>         char *pi, *pj, *pk;
>
>         j = n/6 * p->es;
> @@ -69,9 +69,9 @@
>  }
>
>  static void
> -qsorts(char *a, long n, Sort *p)
> +qsorts(char *a, usize n, Sort *p)
>  {
> -       long j, es;
> +       usize j, es;
>         char *pi, *pj, *pn;
>
>         es = p->es;
> @@ -111,7 +111,7 @@
>  }
>
>  void
> -qsort(void *va, long n, long es, int (*cmp)(void*, void*))
> +qsort(void *va, usize n, usize es, int (*cmp)(void*, void*))
>  {
>         Sort s;
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] make arrays big: qsort edition
  2021-07-29 23:15 ` Nick Owens
@ 2021-07-30 17:24   ` Steve Simon
  2021-08-01 23:53   ` ori
  1 sibling, 0 replies; 6+ messages in thread
From: Steve Simon @ 2021-07-30 17:24 UTC (permalink / raw)
  To: 9front

what is the meaning of usize?

what is is meant for.

-Steve


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] make arrays big: qsort edition
  2021-07-29 23:15 ` Nick Owens
  2021-07-30 17:24   ` Steve Simon
@ 2021-08-01 23:53   ` ori
  2021-08-02 16:14     ` cinap_lenrek
  1 sibling, 1 reply; 6+ messages in thread
From: ori @ 2021-08-01 23:53 UTC (permalink / raw)
  To: 9front

Quoth Nick Owens <mischief@offblast.org>:
> are you able to test this change with a very large array?

Yes, but not easily. This is work towards
fixing that.

Here's a program that creates a large array
using brk.

#include <u.h>
#include <libc.h>

extern char end[];
usize sorted;

int
intcmp(void *a, void *b)
{
	sorted++;
	return *(int*)b-*(int*)a;
}

void
main(int argc, char **argv)
{
	usize n;
	int *p;

	n = 8ULL*1024*1024*1024;
	if(brk(end + sizeof(int)*n) == -1)
		sysfatal("brk: %r");
	p = (void*)end;
	qsort(p, n, sizeof(int), intcmp);
	fprint(2, "%zd\n", sorted);
}


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] make arrays big: qsort edition
  2021-08-01 23:53   ` ori
@ 2021-08-02 16:14     ` cinap_lenrek
  2021-08-03  4:11       ` ori
  0 siblings, 1 reply; 6+ messages in thread
From: cinap_lenrek @ 2021-08-02 16:14 UTC (permalink / raw)
  To: 9front

sbrk() has been fixed to take usize now,
so you can just do: buf = sbrk(LARGE);

--
cinap

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] make arrays big: qsort edition
  2021-08-02 16:14     ` cinap_lenrek
@ 2021-08-03  4:11       ` ori
  0 siblings, 0 replies; 6+ messages in thread
From: ori @ 2021-08-03  4:11 UTC (permalink / raw)
  To: 9front

Quoth cinap_lenrek@felloff.net:
> sbrk() has been fixed to take usize now,
> so you can just do: buf = sbrk(LARGE);

Ah, good to know!

Anyways, updated so we can build the kernel:

if there are no objections, will commit in the
next day or two.

--- /mnt/git/object/7f832df53d2a1d45fb7c7b2129e56b6b9e855682/tree/sys/include/libc.h
+++ sys/include/libc.h
@@ -424,7 +424,7 @@
 extern	int	postnote(int, int, char *);
 extern	double	pow10(int);
 extern	int	putenv(char*, char*);
-extern	void	qsort(void*, long, long, int (*)(void*, void*));
+extern	void	qsort(void*, usize, usize, int (*)(void*, void*));
 extern	int	setjmp(jmp_buf);
 extern	double	strtod(char*, char**);
 extern	long	strtol(char*, char**, int);
--- /mnt/git/object/7f832df53d2a1d45fb7c7b2129e56b6b9e855682/tree/sys/src/9/port/lib.h
+++ sys/src/9/port/lib.h
@@ -158,7 +158,7 @@
 extern	int	dec64(uchar*, int, char*, int);
 extern	int	dec16(uchar*, int, char*, int);
 extern	int	encodefmt(Fmt*);
-extern	void	qsort(void*, long, long, int (*)(void*, void*));
+extern	void	qsort(void*, usize, usize, int (*)(void*, void*));
 
 /*
  * Syscall data structures
--- /mnt/git/object/7f832df53d2a1d45fb7c7b2129e56b6b9e855682/tree/sys/src/libc/port/qsort.c
+++ sys/src/libc/port/qsort.c
@@ -8,12 +8,12 @@
 struct
 {
 	int	(*cmp)(void*, void*);
-	void	(*swap)(char*, char*, long);
-	long	es;
+	void	(*swap)(char*, char*, usize);
+	usize	es;
 } Sort;
 
 static	void
-swapb(char *i, char *j, long es)
+swapb(char *i, char *j, usize es)
 {
 	char c;
 
@@ -27,7 +27,7 @@
 }
 
 static	void
-swapi(char *ii, char *ij, long es)
+swapi(char *ii, char *ij, usize es)
 {
 	long *i, *j, c;
 
@@ -42,9 +42,9 @@
 }
 
 static	char*
-pivot(char *a, long n, Sort *p)
+pivot(char *a, usize n, Sort *p)
 {
-	long j;
+	usize j;
 	char *pi, *pj, *pk;
 
 	j = n/6 * p->es;
@@ -69,9 +69,9 @@
 }
 
 static	void
-qsorts(char *a, long n, Sort *p)
+qsorts(char *a, usize n, Sort *p)
 {
-	long j, es;
+	usize j, es;
 	char *pi, *pj, *pn;
 
 	es = p->es;
@@ -111,7 +111,7 @@
 }
 
 void
-qsort(void *va, long n, long es, int (*cmp)(void*, void*))
+qsort(void *va, usize n, usize es, int (*cmp)(void*, void*))
 {
 	Sort s;
 



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-03  9:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 17:14 [9front] make arrays big: qsort edition ori
2021-07-29 23:15 ` Nick Owens
2021-07-30 17:24   ` Steve Simon
2021-08-01 23:53   ` ori
2021-08-02 16:14     ` cinap_lenrek
2021-08-03  4:11       ` ori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).