aboutsummaryrefslogtreecommitdiff
path: root/round2_32.go
diff options
context:
space:
mode:
authorGravatar Erik Dubbelboer <erik@dubbelboer.com> 2023-07-08 12:40:36 +0200
committerGravatar Erik Dubbelboer <erik@dubbelboer.com> 2023-07-08 12:40:36 +0200
commit1c85d43dfea23d49285c8248aff906c965062031 (patch)
tree38d827e13610680cf36ed01c4354490d7734c492 /round2_32.go
parentAvoid nolint:errcheck in header tests (#1589) (diff)
downloadfasthttp-1c85d43dfea23d49285c8248aff906c965062031.tar.gz
fasthttp-1c85d43dfea23d49285c8248aff906c965062031.tar.bz2
fasthttp-1c85d43dfea23d49285c8248aff906c965062031.zip
Fix round2
- don't limit it to 32 bits - give it a proper name - don't over-allocate too much
Diffstat (limited to 'round2_32.go')
-rw-r--r--round2_32.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/round2_32.go b/round2_32.go
new file mode 100644
index 0000000..541b85e
--- /dev/null
+++ b/round2_32.go
@@ -0,0 +1,29 @@
+//go:build !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x
+// +build !amd64,!arm64,!ppc64,!ppc64le,!s390x
+
+package fasthttp
+
+func roundUpForSliceCap(n int) int {
+ if n <= 0 {
+ return 0
+ }
+
+ // Above 100MB, we don't round up as the overhead is too large.
+ if n > 100*1024*1024 {
+ return n
+ }
+
+ x := uint32(n - 1)
+ x |= x >> 1
+ x |= x >> 2
+ x |= x >> 4
+ x |= x >> 8
+ x |= x >> 16
+
+ // Make sure we don't return 0 due to overflow, even on 32 bit systems
+ if x >= uint32(math.MaxInt32) {
+ return math.MaxInt32
+ }
+
+ return int(x + 1)
+}