Skip to content

Loading…

remove alloca() #1609

Merged
merged 1 commit into from

3 participants

@WalterBright
D Programming Language member

because alloca() is badly supported by many C compilers

@andralex andralex merged commit c01cafa into D-Programming-Language:master

1 check passed

Details default Pass: 10
@andralex
D Programming Language member

merged

@ghost

merged

Just a FYI github notifies people when it's merged. (from recently IIRC).

@andralex
D Programming Language member

Finally! No more manual msg needed.

@braddr
D Programming Language member

This change set seems like a net-negative. What C compiler has problems with it? I haven't seen any bug reports that suggest this code is failing. The pattern ought to be encapsulated as it's not the first place you've done this. That encapsulation starts to suggest that std::string is likely worth considering as it does this sort of thing already.

@WalterBright WalterBright deleted the WalterBright:refactor8 branch
@WalterBright
D Programming Language member

The problem is that various C compilers require system-specific include files. Encapsulating it doesn't work too well, because in order to work in a function alloca() must be used in that function, rather than in some called function.

@WalterBright
D Programming Language member

@braddr: note line 14 in the diff, the __sun include.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 3, 2013
  1. @WalterBright

    remove alloca()

    WalterBright committed
Showing with 19 additions and 19 deletions.
  1. +19 −19 src/mtype.c
View
38 src/mtype.c
@@ -11,12 +11,7 @@
#define __C99FEATURES__ 1 // Needed on Solaris for NaN and more
#define __USE_ISOC99 1 // so signbit() gets defined
-#if defined (__sun)
-#include <alloca.h>
-#endif
-
#include <math.h>
-
#include <stdio.h>
#include <assert.h>
#include <float.h>
@@ -2184,9 +2179,6 @@ Identifier *Type::getTypeInfoIdent(int internal)
{
// _init_10TypeInfo_%s
OutBuffer buf;
- Identifier *id;
- char *name;
- size_t len;
if (internal)
{ buf.writeByte(mangleChar[ty]);
@@ -2195,19 +2187,27 @@ Identifier *Type::getTypeInfoIdent(int internal)
}
else
toDecoBuffer(&buf);
- len = buf.offset;
- name = (char *)alloca(19 + sizeof(len) * 3 + len + 1);
+
+ size_t len = buf.offset;
buf.writeByte(0);
-#if TARGET_OSX
- // The LINKc will prepend the _
- sprintf(name, "D%dTypeInfo_%s6__initZ", 9 + len, buf.data);
-#else
+
+ // Allocate buffer on stack, fail over to using malloc()
+ char namebuf[40];
+ size_t namelen = 19 + sizeof(len) * 3 + len + 1;
+ char *name = namelen <= sizeof(namebuf) ? namebuf : (char *)malloc(namelen);
+ assert(name);
+
sprintf(name, "_D%dTypeInfo_%s6__initZ", 9 + len, buf.data);
-#endif
- if (global.params.isWindows && !global.params.is64bit)
- name++; // C mangling will add it back in
//printf("name = %s\n", name);
- id = Lexer::idPool(name);
+ assert(strlen(name) < namelen); // don't overflow the buffer
+
+ size_t off = 0;
+ if (global.params.isOSX || global.params.isWindows && !global.params.is64bit)
+ ++off; // C mangling will add '_' back in
+ Identifier *id = Lexer::idPool(name + off);
+
+ if (name != namebuf)
+ free(name);
return id;
}
@@ -5354,7 +5354,7 @@ void TypeFunction::toDecoBuffer(OutBuffer *buf, int flag)
Parameter::argsToDecoBuffer(buf, parameters);
//if (buf->data[buf->offset - 1] == '@') halt();
buf->writeByte('Z' - varargs); // mark end of arg list
- if (next != NULL)
+ if(next != NULL)
next->toDecoBuffer(buf);
inuse--;
}
Something went wrong with that request. Please try again.