blob: ea86016561ec1c76d6104a93b98ec7afaafbded7 [file] [log] [blame]
eugenebut064f5bf2015-04-14 18:35:051// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/web/alloc_with_zone_interceptor.h"
6
7#import <objc/runtime.h>
8
9#include "base/logging.h"
10
11namespace web {
12
13void AddAllocWithZoneMethod(Class target, id (^impl_block)(Class, NSZone*)) {
14 // Make sure |allocWithZone:| is not already implemented in the target class.
15 Class meta_class = object_getClass(target);
16 DCHECK_EQ(
17 class_getMethodImplementation(meta_class, @selector(allocWithZone:)),
18 class_getMethodImplementation(object_getClass([NSObject class]),
19 @selector(allocWithZone:)));
20
21 IMP new_impl = imp_implementationWithBlock(^(id self, NSZone* zone) {
22 return impl_block(self, zone);
23 });
24 class_addMethod(meta_class, @selector(allocWithZone:), new_impl, "v@:@");
25}
26
27} // namespace web